I just want to have an object Instantiate another object behind itself, relative to the direction it is facing, not relative to the world. The created object destroys itself if not near the object that made it. I think the destorying self is causing problems. I did drag the object to the slot in the inspector, so I don’t know why I get the null error.
I can Instantiate normally at local 0,0,0, but if I try to move the position I get errors.
This works:
Instantiate(blocketObj, transform.position, transform.rotation); //But isn’t what I want.
This Doesn’t:
Instantiate(blocketObj, transform.position + transform.back * 1.5, transform.rotation);
NullReferenceException: Object reference not set to an instance of an object
This Doesn’t:
var blocketObj : GameObject;
blocketObj = Instantiate(blocketObj, transform.position, transform.rotation);
blocketObj.position.z = -1.5;
MissingReferenceException: The object of type ‘GameObject’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
The null error has nothing to do with where the object is being spawned. It’s all about something not being set. Is the error originating from that line, or is it elsewhere and being caused by the newly instantiated object?
I think we need to see more code. What’s there really doesn’t shed any light on what might be causing the error.
I’m still interested to know how it got you that particular error. Unless there’s a thing in there called “back” that should have given you a compile time error, not a runtime error.
All I know is that changing it from back to forward with a negative value made the error stop. It’s always possible that there was something else in the mix, but my brain can’t seem to absorb what that would be.