Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Requesting help with "UnassignedReferenceException" error

Discussion in 'Scripting' started by gribbly, Oct 10, 2010.

  1. gribbly

    gribbly

    Joined:
    May 2, 2009
    Posts:
    13
    I'm getting this error on startup:

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

    Code (csharp):
    1. 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:

    Code (csharp):
    1.  
    2. var dudeTemplate:GameObject;
    3.  
    4. private var dudeSpawns:Array;
    5.  
    6. function Start() {
    7.     SpawnDudes();
    8. }
    9.  
    10. function SpawnDudes() {
    11.     dudeSpawns = GameObject.FindGameObjectsWithTag("dudespawn");
    12.  
    13.     for(var ds:GameObject in dudeSpawns ) {
    14.         Instantiate(dudeTemplate, ds.transform.position, ds.transform.rotation);
    15.     }
    16. }
    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?
     
  2. Vicenti

    Vicenti

    Joined:
    Feb 10, 2010
    Posts:
    664
    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.
     
  3. gribbly

    gribbly

    Joined:
    May 2, 2009
    Posts:
    13
    OK, I fixed it with:

    Code (csharp):
    1.  
    2. if(dudeTemplate)
    3.     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:

    Code (csharp):
    1. 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)
     
  4. gribbly

    gribbly

    Joined:
    May 2, 2009
    Posts:
    13
    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...
     
    Last edited: Oct 10, 2010
  5. gribbly

    gribbly

    Joined:
    May 2, 2009
    Posts:
    13
    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.
     
  6. Vicenti

    Vicenti

    Joined:
    Feb 10, 2010
    Posts:
    664
    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. :)