Requesting help with "UnassignedReferenceException" error

I’m getting this error on startup:

I have dudeTemplate exposed at the top of LevelScript.js:

var dudeTemplate:GameObject;

…and I’ve dragged and dropped a game object onto it in the Inspector. What’s really weird is that I use spacerTemplate to spawn objects later in the level, and it seems to work fine despite the error message. After searching the forums I tried making the dudeTemplate game object a prefab, but that didn’t change anything so I reverted it.

Here’s the code:

var dudeTemplate:GameObject;

private var dudeSpawns:Array;

function Start() {
    SpawnDudes();
}

function SpawnDudes() {
    dudeSpawns = GameObject.FindGameObjectsWithTag("dudespawn");

    for(var ds:GameObject in dudeSpawns ) {
        Instantiate(dudeTemplate, ds.transform.position, ds.transform.rotation);
    }
}

What’s really weird is that looking at the console output (I have Debug.Logs sprinkled through that I omitted here) it appears that SpawnDudes() is getting called twice. The first time finds my three “dudespawn” objects and succesfully spawns a dudeTemplate on each. But then it immediately gets called again (I have no idea why that would be… I have just one call to it in Start()) and it throws the NullReferenceException when trying to spawn the second dudeTemplate for the second time.

I must be missing something! Can anyone help?

You’ve attached the script to two things.

In Start, Debug.Log( gameObject.name + ", child of " + transform.parent.name ) and you can see which two objects are calling the function.

OK, I fixed it with:

if(dudeTemplate)
    Instantiate(dudeTemplate, ds.transform.position, ds.transform.rotation);

…which gets rid of the error (yay!), but I still don’t know why it was an error in the first place. I’m guessing order of operations - maybe I’m trying to Instantiate dudeTemplate for its properly initialized? Also, the order of msgs in the console doesn’t seem guaranteed (?) which could also be confusing me… hmm I really should timestamp my debug logs.

Finally, I noted that all the Instantiate examples in the documentation assign the return value, like this:

var rocketClone : Rigidbody = Instantiate(rocket, transform.position, transform.rotation);

…but I’m not doing that since I have no need of it. Could that be causing a problem (I don’t see why it would… but not much of this is making sense to me right now)

Hmm, thanks let me check that out…

OK, I pasted that line in at the top of Start(), and it didn’t seem to work. I get:

…although I get it twice which supports your theory. I wonder if I accidentally dropped my LevelScript.js on something in the Hierarchy or Project window…

Well look at that, you were absolutely right Vicenti. I had somehow attached my LevelScript to a completely spurious object. I deleted the duplicate and I’m all good.

Thanks a bunch for the help, that was awesome.

Ah, sorry, the culprit was the ‘transform.parent’ in mine; since your objects didn’t have parent that threw an exception >_<

Glad to be of help, though. :slight_smile: