我正在尝试使用Unity引擎学习C#
但是像这样的基本脚本:
using UnityEngine;
using System.Collections;
public class scriptBall : MonoBehaviour {
// Use this for initialization
void Start () {
Rigidbody.AddForce(0,1000f,0);
}
// Update is called once per frame
void Update () {
}
}
给出此错误:
资产/脚本/scriptBall.cs(8,27):错误CS0120:访问非静态成员UnityyEngine.Rigidbody.AddForce(UnityEngine.Vector3,UnityEngine.ForceMode)时需要对象引用
我找不到我的问题的解决方案
最佳答案
您需要在访问诸如Rigidbody
之类的非静态字段之前实例化您的类AddForce
。
从下面的文档中:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public float thrust;
public Rigidbody rb;
void Start() {
// Get the instance here and stores it as a class member.
rb = GetComponent<Rigidbody>();
}
void FixedUpdate() {
// Re-use the member to access the non-static method
rb.AddForce(transform.forward * thrust);
}
}
更多内容:http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
关于c# - Unity需要对象引用才能访问非静态成员C#,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30850751/