Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# (15,1):错误CS0120:非静态字段、方法或属性“GameObject.GetComponent(string)”需要对象引用_C#_Unity3d - Fatal编程技术网

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