I am sure this is easy, but I have been having an extremely hard time making this happen. I have been trying to make it so that I can shoot a bullet which I have instanciated. Then when it hits the wall, I want it to send a message to an object telling it to move to the position when the bullet hit. I have tried multiple ways of doing this, but I always get a null reference exception as an error. Here was my first code:
function OnCollisionEnter(col : Collision)
transform.position = bulletPosition;
var go = GameObject.Find("object");
I also tried for that last bit
go.transform.position = bulletPosition
But I always get NULL REFERENCE EXCEPTION.
What should I do?
A null reference exception usually gets generated when you're accessing an object that either isn't there anymore, or isn't there yet.
From the scripting reference's description of `GameObject.Find`: "If no game object with name can be found, null is returned".
While it's valid to assign null to a variable, trying to access any of that variable's fields, properties or methods (i.e. its guts) will generate this exception.
Most likely, what this means is that `Find` didn't actually find any such objects and as such returned a `null` value.
You were correct in using `go.transform.position =`... rather than `Translate` because that one translates BY (a direction vector), rather than TO (a position vector). Not sure exactly what bulletPosition represents here but right now you're moving the bullet itself (since this script obviously is attached to the bullet if you're checking collision vs the wall) to that position, and then assigning that position this other mystery object of yours.