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

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)”

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RoomSpawner : MonoBehaviour
{
    public int openingDirection;
    // 1 --> need bottom door
    // 2 --> need top door
    // 3 --> need left door
    // 4 --> need right door

    private RoomTemplates templates;
    private int rand;
    private bool spawned = false;

    public float waitTime = 4f;

    void Start()
    {
        Destroy(gameObject, waitTime);
        templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();
        Invoke("Spawn", 0.1f);
    }

    void Spawn()
    {
        if(spawned == false)
        {
            if (openingDirection == 1)
            {
                //Need to spawn a room with a BOTTOM door.
                rand = Random.Range(0, templates.bottomRooms.Length);
                Instantiate(templates.bottomRooms[rand], transform.position, templates.bottomRooms[rand].transform.rotation);
            }
            else if (openingDirection == 2)
            {
                //Need to spawn a room with a TOP door.
                rand = Random.Range(0, templates.topRooms.Length);
                Instantiate(templates.topRooms[rand], transform.position, templates.topRooms[rand].transform.rotation);
            }
            else if (openingDirection == 3)
            {
                //Need to spawn a room with a LEFT door.
                rand = Random.Range(0, templates.leftRooms.Length);
                Instantiate(templates.leftRooms[rand], transform.position, templates.leftRooms[rand].transform.rotation);
            }
            else if (openingDirection == 4)
            {
                //Need to spawn a room with a RIGHT door.
                rand = Random.Range(0, templates.rightRooms.Length);
                Instantiate(templates.rightRooms[rand], transform.position, templates.rightRooms[rand].transform.rotation);
            }
            spawned = true;
        }
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if(other.CompareTag("SpawnPoint"))
        {
            if(other.GetComponent<RoomSpawner>().spawned == false && spawned == false) 
            {
                Instantiate(templates.closedRoom, transform.position, Quaternion.identity);
                Destroy(gameObject);
            }
            spawned = true;
        }
    }
}

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.

1 Like

Ok, ill try that

1 Like

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.

I solved the problem, what i had to do is link the room spawner code to a spawn point which didn’t had it.

1 Like