- Home /
There is no 'Rigidbody' attached to the "Cube" game object, but a script is trying to access it,There is no Rigidbody attached to the Cube game object.
MissingComponentException: There is no 'Rigidbody' attached to the "Cube" game object, but a script is trying to access it. You probably need to add a Rigidbody to the game object "Cube". Or your script needs to check if the component is attached before using it.
I receive this error message whenever i try to run this code:
#pragma strict
var rotationSpeed= 100;
function Update ()
{
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
rotation *= Time.deltaTime;
GetComponent.().AddRelativeTorque (Vector3.back * rotation);
}
How can i fix it????????? HOW?
The error can't be any clearer about it's own fix. Does the cube you attached this script to have a Rigidbody? If not, add one.
Answer by Zitoox · Oct 25, 2016 at 09:47 PM
Add a RigidBody to your GameObject. Pretty simple. If you have any trouble, please reply this answer.
Hi! I know this is an old thread but i am working on a 2d game, and i get the same error. the object has a 2d rigidbody, and the script calls for a Rigidbody2D. I don't know what is wrong with it.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : $$anonymous$$onoBehaviour {
public float speed = 10f;
public float rotationSpeed = 100f;
private Rigidbody2D rb;
void Start () {
rb = GetComponent<Rigidbody2D> ();
}
void Update () {
if (Input.Get$$anonymous$$eyDown("w")){
rb.AddForce(transform.up * speed);
}
}
}
check the following:
- It IS a Rigidbody2D
- It exists during gameplay
- this script is not accidentally attached to a GO without one