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

[FIXED]NullReferenceException when no reference is null or test for null doesent work

Discussion in 'Scripting' started by PepeBigotes, May 5, 2022.

  1. PepeBigotes

    PepeBigotes

    Joined:
    Dec 19, 2021
    Posts:
    2
    [SOLUTION] unity needs a little delay to instatiate a prefab and his components, if you try to use GetComponent it may not detect it, add a WaitForSeconds for 0.1f secondd before the getcomponent and that should work.

    I have this coroutine for spawning zombies in my game. I got a NullReferenceException error and i tried to identify the null reference via debug logs, but only the NullReferenceException error pops up when I run the game, the other logs dont show up. And I cant fix it cuz i dont know which is the null reference. Im missing something ?
    Code (CSharp):
    1.     IEnumerator SpawnZomb(Transform input){
    2.         ZombsLeft2Spawn -=1;
    3.         IsSpawningZomb = true;
    4.         yield return new WaitForSeconds(1.5f);
    5.         GameObject nowZomb = Instantiate(ZombPrefab , input.position , Quaternion.identity);
    6.         ZombBeh cacheBeh = nowZomb.GetComponent<ZombBeh>();
    7.         if (cacheBeh == null) Debug.Log("null error"); //this log doesent trigger
    8.         if (ProggScript == null) Debug.Log("null error progg"); // this log doesent trigger
    9.         cacheBeh.SetSpeed(ProggScript.GetRound() * 0.04f + 1.5f ); //ERROR HERE
    10.         cacheBeh.SetHealth(ProggScript.GetRound()^2 + ProggScript.GetRound()*100 );
    11.         yield return new WaitForSeconds(0.5f);
    12.         ZombsAlive += 1;
    13.         IsSpawningZomb = false;
    14.     }
    [SerializeField] private Progg ProggScript; up btw, i asigned it in the editor.

    Edit: just tried if (input == null) Debug.Log("input is null");
    and it didnt work.

    Edit 2: full error message:
    NullReferenceException: Object reference not set to an instance of an object
    ZombSpawner+<SpawnZomb>d__12.MoveNext () (at Assets/Scripts/ZombSpawner.cs:50)
    UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <82c503977c5347cf82f44677f633fcf6>:0)


    full script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ZombSpawner : MonoBehaviour{
    6. [SerializeField] private Progg ProggScript;
    7. [SerializeField] private ZoneCheck MainZCScript;
    8. [SerializeField] private GameObject ZombPrefab;
    9. private int ZombsLeft2Spawn;
    10. private int ZombsAlive;
    11. private int MaxZombs = 20;
    12. private bool RoundChanging = false;
    13. private bool IsSpawningZomb = false;
    14. [SerializeField] Transform[] DemoMapSpawns;
    15. private Transform CacheTransform;
    16.  
    17.  
    18.  
    19.     void Update(){
    20.         if (ProggScript.GetMap() == 0) return;
    21.         if(ZombsAlive < MaxZombs && !RoundChanging && !IsSpawningZomb){
    22.             SpawnZombAtMap(ProggScript.GetMap() );
    23.         }
    24.      
    25.     }
    26.     void SpawnZombAtMap(int map){
    27.         switch (map) {
    28.             case 1:
    29.             CacheTransform = DemoMapSpawns[MainZCScript.GetZone()*4 + Random.Range(0 , 5)];
    30.             break;
    31.         }
    32.         StartCoroutine(SpawnZomb(CacheTransform) );
    33.         if (CacheTransform == null){
    34.             Debug.Log("CacheSpawnTransform is null");
    35.         }
    36.     }
    37.     IEnumerator SpawnZomb(Transform input){
    38.         //https://forum.unity.com/threads/nullreferenceexception-when-no-reference-is-null-or-test-for-null-doesent-work.1277489/
    39.         if (input == null) Debug.Log("input for SpawnZomb is null");
    40.         if (ZombPrefab == null) Debug.Log("zomb prefab null");
    41.         ZombsLeft2Spawn -=1;
    42.         IsSpawningZomb = true;
    43.         //GameObject nowAnim = Instatiate(AnimPrefab , input.position , Quaternion.identity);
    44.         //Destroy(nowAnim , 1.5f);
    45.         yield return new WaitForSeconds(1.5f);
    46.         GameObject nowZomb = Instantiate(ZombPrefab , input.position , Quaternion.identity);
    47.         ZombBeh cacheBeh = nowZomb.GetComponent<ZombBeh>();
    48.         if (cacheBeh == null) Debug.Log("null error");
    49.         if (ProggScript == null) Debug.Log("null error progg");
    50.         cacheBeh.SetSpeed(ProggScript.GetRound() * 0.04f + 1.5f );
    51.         cacheBeh.SetHealth(ProggScript.GetRound()^2 + ProggScript.GetRound()*100 );
    52.         yield return new WaitForSeconds(0.5f);
    53.         ZombsAlive += 1;
    54.         IsSpawningZomb = false;
    55.     }
    56.     public void AddZombs2Spawn(int input = 1){
    57.         ZombsLeft2Spawn += input;
    58.     }
    59.     public void MinusAliveZomb(){
    60.         ZombsAlive -= 1;
    61.     }
    62.     public int GetZombsAlive(){
    63.         return ZombsAlive;
    64.     }
    65.     public void SetZombsAlive(int input){
    66.         ZombsAlive = input;
    67.     }
    68.     public int GetZombsLeft2Spawn(){
    69.         return ZombsLeft2Spawn;
    70.     }
    71.     public void SetZombs2Spawn(int input){
    72.         ZombsLeft2Spawn = input;
    73.     }
    74.     public void SetRoundChanging(bool input){
    75.         RoundChanging = input;
    76.     }
    77. }
    78.  
     
    Last edited: May 8, 2022
  2. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    491
    If you get an error and want people on the forum to help, you will need to provide the exact error message because that contains the script reference and the line numbers to check.

    You will also need to provide the full script in code tags so that we can check exactly where the error points to, as well as seeing any surrounding code.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,963
  4. PepeBigotes

    PepeBigotes

    Joined:
    Dec 19, 2021
    Posts:
    2
    thats the problem, im having trouble identifying the null object. Im creating prints for every reference in the script and they dont seem to trigger.