【问题标题】:Error: an object reference is required to access non static member错误:访问非静态成员需要对象引用
【发布时间】:2015-11-01 00:25:59
【问题描述】:
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);
    }
}

【问题讨论】:

  • 您应该添加有关您的问题的更多信息!你想达到什么目的?你有没有尝试其他方法?

标签: c# unity3d


【解决方案1】:

自 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);
    }
}

【讨论】:

    猜你喜欢
    • 2014-11-04
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    相关资源
    最近更新 更多