- Home /
Question by
kingtyler1 · Apr 03, 2015 at 12:26 AM ·
javascriptrigidbody2d
How to assign Rigidbody2d to a player using Javascript
I am trying to connect Rigidbody2d to a cube named Player so that it is able to jump. This is my code so far... #pragma strict
function Start () {
}
function Update () { if (Input.GetKeyDown(KeyCode.Space)); { GetComponent(Rigidbody2D).velocity = new Vector2(0,10); }
} When I try to play my game this comes up...
MissingComponentException: There is no 'Rigidbody2D' attached to the "Player" game object, but a script is trying to access it. What do I do to fix this. I am using Javascript.
Comment
Answer by jcv8000 · Apr 03, 2015 at 02:15 PM
Either add a Rigidbody2D component to the cube in the Editor, or put this in the script:
function Start()
{
AddComponent(Rigidbody2D);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613192025im_/https://answers.unity.com/themes/thub/images/avi.jpg)