Search Unity

NullReferenceException - strange problem instantiating prefab

Discussion in 'Editor & General Support' started by VeraxOdium, Sep 1, 2011.

  1. VeraxOdium

    VeraxOdium

    Joined:
    Jul 2, 2011
    Posts:
    233
    I'm not sure if I've lost my mind and I'm doing something stupid that I'm just not seeing but here is the problem. I started having trouble with this bug in my game and decided to start a new project with just the barebones setup needed to reproduce it.

    All I'm doing is very simple.

    1. Imported mesh.
    2. Created prefab "testPrefab" of mesh.
    3. Created "Game" empty gameobject in Hierarchy.
    4. Created "Game.js" script and attached it to "Game" object in Hierarchy.

    Code (csharp):
    1.  
    2. var test : Transform;
    3.  
    4. function Start()
    5. {
    6.     print("test = " + test);
    7.     Instantiate(test, Vector3(0,0,0), Quaternion.Identity);
    8. }
    9.  
    I've assigned the "test" prefab to the target variable exposed in "Game".

    In the console the print function prints this

    Code (csharp):
    1.  
    2. test = testPrefab (UnityEngine.Transform)
    3. UnityEngine.MonoBehaviour:print(Object)
    4. Game:Start() (at Assets/Game.js:5)
    5.  
    Everything seems fine there, I think, it sees it right ?!?!? But then I get

    Code (csharp):
    1.  
    2. NullReferenceException: Object reference not set to an instance of an object
    3. Game.Start () (at Assets/Game.js:6)
    4.  
    I assume I'm making some silly blunder but, I've been doing things similar to this for months now without trouble, I just don't understand.

    I've tried restarting Unity, rebooting computer, completely new project, importing a different mesh... I don't know, any ideas?
     
  2. VeraxOdium

    VeraxOdium

    Joined:
    Jul 2, 2011
    Posts:
    233
    Sigh, a silly mistake... Quaternion.identity, not Quaternion.Identity... wish the compiler error said "hey I don't know what Quaternion.Identity is".
     
  3. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,822
    Use C# and Visual Studio's intellisense and you'll never have a problem like that again.