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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

(SOLVED) Getting NullReferenceException for seemingly no reason?

Discussion in 'Scripting' started by CannedSmeef, Oct 3, 2019.

  1. CannedSmeef

    CannedSmeef

    Joined:
    Sep 29, 2018
    Posts:
    28
    I'm trying to spawn in prefabs using Object.Instantiate from a prefab I pass in through the inspector. Everything else seems to be working except Instantiate, it keeps bringing up a NullReferenceException error, like I'm giving it an empty variable. Yes, I did assign an object in the inspector, and I tried others, but nothing works.

    Code (CSharp):
    1. // I'll only show relevant code
    2.  
    3. // enemy spawning variables
    4.     // enemy spawning variables
    5.     private Vector3[] spawnPoints;
    6.     public GameObject enemyA;
    7.     private float timeSurvived, spawnTimer, timeBetweenSpawns;
    8.     public int tracked, difficulty; // currently alive and the max
    9.  
    10. // ...
    11.  
    12. // update time survived and spawn enemies
    13.     void Update()
    14.     {
    15.         timeSurvived += Time.deltaTime;
    16.         spawnTimer -= Time.deltaTime;
    17.  
    18.         // if the timer is ready and a slot's available
    19.         if (spawnTimer < 0.0f && tracked < difficulty)
    20.         {
    21.             Debug.Log("lmao");
    22.             Object.Instantiate(enemyA, spawnPoints[Random.Range(0, 8)], Quaternion.identity);
    23.             timeBetweenSpawns = 1.0f;
    24.             spawnTimer = timeBetweenSpawns;
    25.             CalculateDifficulty();
    26.             tracked++;
    27.         }
    28.     }
    I'm starting to suspect that this is a Unity bug. Any thing wrong here?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    On your debug log in line 21, try:
    Code (csharp):
    1. Debug.Log("enemyA is "+(enemyA != null ? "not null" : "null", this);
    What does it say there?

    It's possible you have accidentally added the script to another object in your scene; one may have the correct reference assigned, one may not, and the second will be triggering the error. If so, then clicking on the above will take you to the object with the error.
     
  3. FernandoHC

    FernandoHC

    Joined:
    Feb 6, 2018
    Posts:
    337
    Hey, can you change the debug to
    Debug.Log("lmao " + (enemyA == null ? "Im null" : ""+enemyA));

    and tell us what you get?
     
  4. Serellyn

    Serellyn

    Joined:
    Sep 30, 2011
    Posts:
    104
    Have you actually filled your 'spawnpoints' variable?
     
  5. ibbybn

    ibbybn

    Joined:
    Jan 6, 2017
    Posts:
    193
    The nullref might be spawnPoints. Have you tried Debug.Log on all parts of the line?
     
  6. CannedSmeef

    CannedSmeef

    Joined:
    Sep 29, 2018
    Posts:
    28
    "enemyA is (not null, Player (PlayerController))"

    I tried switching out my spawnPoints for just a plain ol' vector3 and suddenly it's working! Here's the code setting them up (it's supposed to be a list of possible points to spawn at):

    Code (CSharp):
    1. // enemy spawning variables
    2.     private Vector3[] spawnPoints;
    3.  
    4. // Start is called before the first frame update
    5.     void Start()
    6.     {
    7.         // create spawn points
    8.         Vector3[] spawnPoints = new Vector3[8];
    9.         spawnPoints[0] = new Vector3(-6.0f, -10.0f, 0.0f);
    10.         spawnPoints[1] = new Vector3(-6.0f, -6.0f, 0.0f);
    11.         spawnPoints[2] = new Vector3(-6.0f, -2.0f, 0.0f);
    12.         spawnPoints[3] = new Vector3(6.0f, -10.0f, 0.0f);
    13.         spawnPoints[4] = new Vector3(6.0f, -6.0f, 0.0f);
    14.         spawnPoints[5] = new Vector3(6.0f, -2.0f, 0.0f);
    15.         spawnPoints[6] = new Vector3(-2.0f, -10.0f, 0.0f);
    16.         spawnPoints[7] = new Vector3(2.0f, -10.0f, 0.0f);
    17.     }
     
    FernandoHC likes this.
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    Your mistake is you have a class variable named spawnPoints, but then you create a local variable named spawnPoints. These are not the same variable.

    So your class variable is empty, your local one gets populated, but then discarded once Start is exited.
    Remove the Vector3[] from the Start variable
     
    StarManta likes this.
  8. CannedSmeef

    CannedSmeef

    Joined:
    Sep 29, 2018
    Posts:
    28
    Success! Thank you everyone, everything is working properly now!