Rigidbody doesn't move unless it's an empty

So, this might not be the right course, but here goes:


When I create an Gameobject using a prefab, say a Droid, I drag it into the Scene, place it yada yada. Now, I add an Rigidbodycomponent to it.

Script looks something like this (typed it up from memory, simply assume all syntax errors aren't present in the actual script, it compiles fine  :) )


public Rigidbody rb;

class

start(){

rb = GetComponent<Rigidbody>();

}

update(){

//The values don't really matter, I tried from 1 to 100000, same result.

rb.AddForce(Vector3.forward * 500f);

transform.Rotate(0,0,1f); 


}

The droid will not move forward, however. It will rotate, no problem. Once I put the Droid-Object into an Empty, add a Rigidbody to the Empty and attach this script (or something similar) to it, it will move. 


Is that a quirk of Unity or does a prefab have to be child to an empty, to move it via addForce?

  • Jonathan Gonzalez(jgonzalez) replied

    When you use "rb = GetComponent<Rigidbody>();" you're telling the script to automatically assigned this component for you. So "rb" should be assigned using this script. This also means that this script must be applied to the object that has a Rigidbody component. Check your console for errors, there's probably an error saying a reference is missing or something of that sort. 

    If you want to apply this to an object that doesn't have Rigidbody or you want to manually assign this yourself then change it to "rb = rb.GetComponent<Rigidbody>();".This lets the script know that the Rigidbody component it needs to get is coming from whatever is assigned to "rb".