2017-01-15 18 views
1

私はC#の新機能ですので、これが明らかであれば私を許してください。Unity Error:UnityEngine.Component 'に' velocity 'の定義が含まれていません

this tutorialの手順に従っており、手順6で問題が発生しました。それが与えるエラーはこれです:それが与える誤差はこれです:

using UnityEngine; 
using System.Collections; 

public class RobotController : MonoBehaviour { 
//This will be our maximum speed as we will always be multiplying by 1 
public float maxSpeed = 2f; 
//a boolean value to represent whether we are facing left or not 
bool facingLeft = true; 
//a value to represent our Animator 
Animator anim; 
// Use this for initialization 
void Start() { 
    //set anim to our animator 
    anim = GetComponent<Animator>(); 

} 

// Update is called once per frame 
void FixedUpdate() { 

    float move = Input.GetAxis ("Horizontal");//Gives us of one if we are moving via the arrow keys 
    //move our Players rigidbody 
    rigidbody2D.velocity = new Vector3 (move * maxSpeed, rigidbody2D.velocity.y); 
    //set our speed 
    anim.SetFloat ("Speed",Mathf.Abs (move)); 
    //if we are moving left but not facing left flip, and vice versa 
    if (move < 0 && !facingLeft) { 

    Flip(); 
    } else if (move > 0 && facingLeft) { 
    Flip(); 
    } 
} 

//flip if needed 
void Flip(){ 
    facingLeft = !facingLeft; 
    Vector3 theScale = transform.localScale; 
    theScale.x *= -1; 
    transform.localScale = theScale; 
} 
} 

エラーがライン23上にある:

に使用
rigidbody2D.velocity = new Vector3 (move * maxSpeed, rigidbody2D.velocity.y); 

答えて

4

rigidbody2Dここ

UnityEngine.Component' does not contain a definition for `velocity' and no extension method `velocity' of type `UnityEngine.Component' could be found. Are you missing an assembly reference?' 

コードですMonoBehaviourが継承するComponentから継承した変数です。現在は推奨されていません。

Start()関数のAnimator(anim)変数と同じように、宣言してGetComponent<Rigidbody>();で初期化する必要があります。また、古い変数と混同しないように、rigidbody2Dの名前を変更することをお勧めします。以下のサンプルコードでは、名前をrigid2Dに変更して宣言します。

あなたはそれの名前を変更しない場合は、という警告を受け取ることがあります。

Severity Code Description Project File Line Suppression State Warning CS0108 'RobotController.rigidbody2D' hides inherited member 'Component.rigidbody2D'. Use the new keyword if hiding was intended.

別にUnityEngineを使用して `除外から
public class RobotController: MonoBehaviour 
{ 
    public float maxSpeed = 2f; 
    //a boolean value to represent whether we are facing left or not 
    bool facingLeft = true; 
    //a value to represent our Animator 
    Animator anim; 

    //Declare rigid2D 
    Rigidbody rigid2D; 
    // Use this for initialization 
    void Start() 
    { 
     //set anim to our animator 
     anim = GetComponent<Animator>(); 

     //Initialize rigid2D 
     rigid2D = GetComponent<Rigidbody>(); 
    } 

    // Update is called once per frame 
    void FixedUpdate() 
    { 

     float move = Input.GetAxis("Horizontal");//Gives us of one if we are moving via the arrow keys 
               //move our Players rigidbody 
     rigid2D.velocity = new Vector3(move * maxSpeed, rigid2D.velocity.y); 
     //set our speed 
     anim.SetFloat("Speed", Mathf.Abs(move)); 
     //if we are moving left but not facing left flip, and vice versa 
     if (move < 0 && !facingLeft) 
     { 

      Flip(); 
     } 
     else if (move > 0 && facingLeft) 
     { 
      Flip(); 
     } 
    } 

    //flip if needed 
    void Flip() 
    { 
     facingLeft = !facingLeft; 
     Vector3 theScale = transform.localScale; 
     theScale.x *= -1; 
     transform.localScale = theScale; 
    } 
} 
+0

、正常に動作します'; 'と'にSystem.Collectionsを使用して。 – TrumpetDude

+1

あなたの質問に既にあなたがいるので、私はそれらを目的から外しました。クラス内のすべてを新しいクラスに置き換えるだけです。 – Programmer

関連する問題