- Home /
The question is answered, right answer was accepted
Why is rigidbody.addForce calling a Rigidbody2D function?
I'm trying to write a script that defines the motion of a player-type character in a 3D game by calling rigidbody.addForce, but the console is telling me that there is no Rigidbody2D attached. I'm aware of this, as there's a Rigidbody attached, not a Rigidbody2D, on purpose. I want this to be a 3D game.
void FixedUpdate () {
if (Input.GetKey (KeyCode.W)) { //forward
rigidbody.AddForce(0, 0, 2);
}
if (Input.GetKey (KeyCode.S)) { //back
rigidbody.AddForce(0, 0, -2);
}
if (Input.GetKey (KeyCode.A)) { //left
rigidbody.AddForce(-2, 0, 0);
}
if (Input.GetKey (KeyCode.D)) { //right
rigidbody.AddForce(2, 0, 0);
}
}
Nothing here should cause that problem. Any references to rigidbody2d in the script anywhere? Any other components attached to the game object that might reference rigidbody2d? What line in $$anonymous$$onodevelop is highlighted if you double click on the error.
Ugh. Found the problem. I had left a different script in the Assets folder that was causing the error. Sorry about the confusion.
[request to close thread]