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. Dismiss Notice

Question I have a error that i don't know how to fix it!

Discussion in '2D' started by EnderAlex10000, Feb 6, 2021.

  1. EnderAlex10000

    EnderAlex10000

    Joined:
    Dec 29, 2020
    Posts:
    3
    I've watched a youtube video on how to make a random dungeon map generator, but when I run it the spawn is blocked and an error keeps popping out.

    the error is :"NullReferenceException: Object reference not set to an instance of an object
    RoomSpawner.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/scripts/RoomSpawner.cs:62)"

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RoomSpawner : MonoBehaviour
    6. {
    7.     public int openingDirection;
    8.     // 1 --> need bottom door
    9.     // 2 --> need top door
    10.     // 3 --> need left door
    11.     // 4 --> need right door
    12.  
    13.     private RoomTemplates templates;
    14.     private int rand;
    15.     private bool spawned = false;
    16.  
    17.     public float waitTime = 4f;
    18.  
    19.     void Start()
    20.     {
    21.         Destroy(gameObject, waitTime);
    22.         templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();
    23.         Invoke("Spawn", 0.1f);
    24.     }
    25.  
    26.     void Spawn()
    27.     {
    28.         if(spawned == false)
    29.         {
    30.             if (openingDirection == 1)
    31.             {
    32.                 //Need to spawn a room with a BOTTOM door.
    33.                 rand = Random.Range(0, templates.bottomRooms.Length);
    34.                 Instantiate(templates.bottomRooms[rand], transform.position, templates.bottomRooms[rand].transform.rotation);
    35.             }
    36.             else if (openingDirection == 2)
    37.             {
    38.                 //Need to spawn a room with a TOP door.
    39.                 rand = Random.Range(0, templates.topRooms.Length);
    40.                 Instantiate(templates.topRooms[rand], transform.position, templates.topRooms[rand].transform.rotation);
    41.             }
    42.             else if (openingDirection == 3)
    43.             {
    44.                 //Need to spawn a room with a LEFT door.
    45.                 rand = Random.Range(0, templates.leftRooms.Length);
    46.                 Instantiate(templates.leftRooms[rand], transform.position, templates.leftRooms[rand].transform.rotation);
    47.             }
    48.             else if (openingDirection == 4)
    49.             {
    50.                 //Need to spawn a room with a RIGHT door.
    51.                 rand = Random.Range(0, templates.rightRooms.Length);
    52.                 Instantiate(templates.rightRooms[rand], transform.position, templates.rightRooms[rand].transform.rotation);
    53.             }
    54.             spawned = true;
    55.         }
    56.     }
    57.  
    58.     void OnTriggerEnter2D(Collider2D other)
    59.     {
    60.         if(other.CompareTag("SpawnPoint"))
    61.         {
    62.             if(other.GetComponent<RoomSpawner>().spawned == false && spawned == false)
    63.             {
    64.                 Instantiate(templates.closedRoom, transform.position, Quaternion.identity);
    65.                 Destroy(gameObject);
    66.             }
    67.             spawned = true;
    68.         }
    69.     }
    70. }
    71.  
     

    Attached Files:

  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    If it cannot find the "RoomSpawner" component then it'll return NULL. Your code assumes it'll be there and then proceeds to call ".spawned" on a NULL reference. You should either ensure there's a component like that and/or change your code to be cautious and check for NULL and throw a warning with some info.
     
    EnderAlex10000 likes this.
  3. EnderAlex10000

    EnderAlex10000

    Joined:
    Dec 29, 2020
    Posts:
    3
    Ok, ill try that
     
    MelvMay likes this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    Not sure if it's worth it here but sometimes it's worth asserting things are in the state you expect so you don't have to make the main body of the code too cautious.

    You can use the Assert API such as this one.
     
  5. EnderAlex10000

    EnderAlex10000

    Joined:
    Dec 29, 2020
    Posts:
    3
    I solved the problem, what i had to do is link the room spawner code to a spawn point which didn't had it.
     
    MelvMay likes this.