Unity 5 中的构建错误

Build error in Unity 5

我是编程的绝对初学者,我正在尝试使用 Unity 5,但是每当我尝试构建此代码时都会收到此错误代码

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour 
{
    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

        Rigidbody.AddForce(movement);
    }
}

我收到 "error cs0120 an object reference is required for the nonstatic field method or property" 谁能帮我解决这个问题?

谢谢!

在 Unity 5 之前,"rigidBody" 是 GameObject 的 属性。您的代码仍然无法编译,它需要是:

gameObject.rigidBody.AddForce(movement);

因为 rigidBody 不是 属性 或 MonoBehavior 的字段,所以 gameObject 是。由于它不在 Unity 5 中,因此您需要使用 GetComponent:

RigidBody rb = GetComponent<RigidBody>();
rb.AddForce(movement);

有关更多信息,请参阅文档:Unity Docs

当一切都说完之后,代码将是:

    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

        RigidBody rb = GetComponent<RigidBody>();
        rb.AddForce(movement);
    }