-2

I am programming

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

and I am getting this problem:

(15,1): error CS0120: An object reference is required for the non-static field, method, or property 'GameObject.GetComponent(string)'

GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
PIEGENIX
  • 3
  • 1
  • 5

1 Answers1

0

GameObject is the name of a class. The error is telling you that an object is expected, in other words a particular instance of GameObject, such as the player.

eg

GameObject player = GameObject.Find("player");
player.GetComponent("RigidBody2D").AddForce(new Vector2(0f,5f),ForceMode2D.Impulse);
GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103