C# (15,1):错误CS0120:非静态字段、方法或属性“GameObject.GetComponent(string)”需要对象引用
我正在编程C# (15,1):错误CS0120:非静态字段、方法或属性“GameObject.GetComponent(string)”需要对象引用,c#,unity3d,C#,Unity3d,我正在编程 using UnityEngine; public class playermove : MonoBehaviour { public float moveSpeed=5f; // Update is called once per frame void Update() { jump(); Vector3 movment = new Vector3(Input.GetAxis("Horizontal"),0f ,0
using UnityEngine;
public class playermove : MonoBehaviour
{
public float moveSpeed=5f;
// Update is called once per frame
void Update()
{
jump();
Vector3 movment = new Vector3(Input.GetAxis("Horizontal"),0f ,0f);
transform.position += movment * Time.deltaTime * moveSpeed;
}
void jump()
{
if (Input.GetButtonDown("jump"))
{
GameObject.GetComponent("RigidBody2D")().AddForce(new Vector2(0f,5f),ForceMode2D.Impulse);
}
}
}
我遇到了这个问题:
15,1:错误CS0120:非静态字段、方法或属性“GameObject.GetComponentstring”需要对象引用。GameObject是类的名称。错误是告诉你一个对象是预期的,换句话说,一个特定的游戏对象实例,比如玩家
乙二醇
GameObject player = GameObject.Find("player");
player.GetComponent("RigidBody2D").AddForce(new Vector2(0f,5f),ForceMode2D.Impulse);