Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

NullReferenceException until tile is spawned

Discussion in 'Scripting' started by onurduha, Feb 16, 2019.

  1. onurduha

    onurduha

    Joined:
    Dec 29, 2018
    Posts:
    51
    I have a tile and tile has enemies spawn positions and spawner script in it everything is works fine when tile is spawned my ray hits the collider in tile and enemies get spawned but my spawn script is in tile my ray script is in my player so im reaching from my player script to spawn script but until my tile is spawned i get this error NullReferenceException: Object reference not set to an instance of an object because tile is not spawned yet after its spawned error stops but after tile is deleted it starts again how can i get rid of this error i tried other ways but it worked with ray in other ways it didn't spawn or spawned random positions.

    Player Script
    Code (CSharp):
    1. private Spawner spawner;
    2. private RaycastHit hit;
    3.  
    4. private void Update()
    5. {
    6.       spawner = GameObject.FindGameObjectWithTag("Spawner").GetComponent<Spawner>();
    7.       DrawRay();
    8. }
    9.  
    10. private void DrawRay()
    11.     {
    12.        
    13.         Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
    14.  
    15.         if(Physics.Raycast(ray,out hit))
    16.         {
    17.             if(hit.collider.tag == "SpawnerCollider")
    18.             {
    19.                 if(hit.distance < 5)
    20.                 {
    21.                     Destroy(hit.collider);
    22.                     spawner.Spawn();
    23.                 }
    24.             }
    25.         }
    26.     }
    Spawner Script
    Code (CSharp):
    1. public Transform[] spawnPos;
    2.     public GameObject fireBall;
    3.  
    4.     public void Spawn()
    5.     {
    6.         for (int i = 0; i < spawnPos.Length; i++)
    7.         {
    8.             Instantiate(fireBall, spawnPos[i].position, Quaternion.identity);
    9.         }
    10.     }
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    It's difficult to follow your post without punctuation.

    Anyway, it seems you already figured out what the problem actually is:
    As long as the tile doesn't exist, you'll get an error.

    And that's because you're trying to access it in Update, no matter whether it exists or not. More specifically, it's this line:
    Code (csharp):
    1. spawner = GameObject.FindGameObjectWithTag("Spawner").GetComponent<Spawner>();
    Before you use the object that's supposed to be returned by 'FindGameObjectWithTag', you want to check the return value against 'null'. In order to do that, you should break that line into multiple commands and wrap the subsequent call to 'GetComponent' in the if statement that's doing the null-check.

    Note that you should avoid code like FindGameObjectWithTag in Update. Instead, you could link the spawner via the inspector, or at least resolve it in Awake / Start and use the cached reference.