C# 统一访问非静态成员C需要对象引用#
我想用Unity engine学习C# 但基本的脚本是这样的:C# 统一访问非静态成员C需要对象引用#,c#,unity3d,C#,Unity3d,我想用Unity engine学习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
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 () {
}
}
给出此错误:
Assets/Scripts/scriptBall.cs(8,27):错误CS0120:访问非静态成员'UnityEngine.Rigidbody.AddForce(UnityEngine.Vector3,UnityEngine.ForceMode)'需要对象引用
我找不到问题的解决方案在访问非静态字段(如
AddForce
)之前,您需要实例化类Rigidbody
从以下文档中:
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);
}
}
使用UnityEngine;
使用系统集合;
公共类示例类:单一行为{
公众推动;
公共刚体;
void Start(){
//在此处获取实例并将其存储为类成员。
rb=GetComponent();
}
void FixedUpdate(){
//重新使用成员以访问非静态方法
rb.附加力(变换向前*推力);
}
}
此处的详细信息:将局部特性添加到刚体,并在编辑器中进行设置或使用
var rigidBody = GetComponenet<RigidBody>();
rigidBody.Addforce(...)
var rigidBody=GetComponenet();
刚体。附加力(…)
通过代码而不是编辑器获取组件的本地实例。
AddForce
是刚体
的实例方法。在调用AddForce
之前,需要一个Rigidbody
的实例。否则,你要给什么添加一个力呢?你可能想给C#示例,而不是Java示例。@RonBeyer傻我:)谢谢!