简体   繁体   中英

There is no 'Rigidbody2D' attached to the "Player" game object, but a script is trying to access it. (Unity)

I am very new to scripting in C# and I am trying to add a component to an object but I keep on getting this error:

There is no 'Rigidbody2D' attached to the "Player" game object, but a script is trying to access it.

This is the code I have so far:

void Start()
{
    GameObject.Find("Player");
    gameObject.AddComponent<Rigidbody2D>();
    Body = GetComponent<Rigidbody2D>();        
}

private void FixedUpdate()
{
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");
    Vector2 movement = new Vector2(moveHorizontal, moveVertical);
    Body.AddForce(movement * speed);
}

Solution

If you are adding script for the rigid body then you should attach a rigid body in the gameobject in the hierarchy view of the project . Click on the agmeobject in hierarchical view of the project . It will show you the inspector view of the gameobject and attach a rigid body to the gameobject and the error will be resolved and its helps us to implement the method of rigid body and physics regarding features .

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM