Error: an object reference is required to access non static member

时间:2015-10-30 22:18:37

标签: c# unity3d

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {
    public float moveSpeed;

    private Vector3 input;

    void Update () {
        input = new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
        rigidbody.AddForce(input * moveSpeed);
    }
}

1 个答案:

答案 0 :(得分:0)

从Unity 5开始UnityEngine.Component.rigibody已被弃用,使用它会导致编译错误。相反,您需要使用GetComponent<Rigidbody>()。因此,在您的情况下,您的代码将如下所示:

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {
public float moveSpeed;

private Vector3 input;

    void Update () 
    {
        input = new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
        GetComponent<Rigidbody>().AddForce(input * moveSpeed);
    }
}