Solved - Instantiate Object Relative Position, Why so hard?

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.

The null error comes from: Instantiate(blocketObj, transform.position + transform.back * 1.5, transform.rotation);

I put the errors under the exact code that the errors pointed to.

I guess I should also mention that I’m trying to use navmesh (free edition) and the function looks like this:

var blocketObj : GameObject;

function Blocket (){
var agent: NavMeshAgent = GetComponent.();
if(agent.velocity.y == 0)
{
//Instantiate(blocketObj, transform.position + (Vector3.back*1.5), transform.rotation);
Instantiate(blocketObj, transform.position + transform.back * 1.5, transform.rotation);
//var blocketObjx = Instantiate(blocketObj, transform.position, transform.rotation);
//blocketObjx.position.z = -7.5; //How fucking hard is it to make an object appear behind itself??
}
}

BTW: What happened to forum code wrapping?

Alright then, what is transform.back? It’s not listed in the documentation for Transform, and it’s unfamiliar to me… (I’d use transform.forward * -1).

Derp.

Okay, yes that works.

I saw “back” under the vector 3 API http://docs.unity3d.com/ScriptReference/Vector3.html I thought I could use it like forwards etc. Silly me.

Thanks, problem fixed.

–Eric

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.