Access Rigidbody from GameObject instantiated in other method?

Hi, super simple thing I’m missing here probably but I’m pretty stuck atm;

I’m instantiating objects in a separate code block (void newObj) that I then need to control the movement of in void update. However since the object is instantiated in its own method I don’t seem to be able to access the rigidbody from update? How would I solve this?

public class SimpleInd : MonoBehaviour {

    public GameObject Individual;

    private Vector3 pos1;

    private int posx = 0;
    private int posy = 0;

    public Rigidbody rbody;

    private float speed = 10f;

    void Start(){

        newObj ();
    }

    void newObj(){

        pos1 = new Vector3 (posx, posy, 0);

        GameObject ind = Instantiate (Individual, pos1, Quaternion.identity) as GameObject;
        rbody = ind.GetComponent<Rigidbody> ();
    }

    void update(){

        rbody.velocity = transform.forward * speed;

    }
}

Update needs to have a capital U.
void Update(){

2 Likes

Oh. Well, thank you. I guess I’ll leave quietly and go scream into a pillow somewhere.

1 Like