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.

How to check variable on obj I triggered

Discussion in 'Visual Scripting' started by Vikoman, Aug 21, 2022.

  1. Vikoman


    Aug 21, 2022
    Hi I'm doing a game whre you have randomly generated dungeon and I want to check if the spawnpoint that spawns rooms has variable "spawned" set to false. I used GetComponent to do this and it is giving me "NullReferenceException" error. Is there anyone who could help me?
    I'm following this tutorial to do the dungeon generation:

    And I'm getting that error after he coded the closedRoom spawn part.

    Code (CSharp):
    2. void OnTriggerEnter(Collider other)
    3.     {
    4.         roomSpawner = other.gameObject.GetComponent<RoomSpawner>();
    5.         if (other.CompareTag("SpawnPoint"))
    6.         {
    7.           if(other.GetComponent<RoomSpawner>().spawned == false && spawned == false)
    8.             {
    9.                 Instantiate(templates.closedRoom[1], transform.position, Quaternion.identity);
    10.                 Destroy(gameObject);
    11.             }
    12.             spawned = true;
    13.         }
    Last edited: Aug 22, 2022