Blackthornprod's random dungeon tutorial, NullReferenceException problem.

As the title says I’m having a problem with ‘NullReferenceException: Object reference not set to an instance of an object’ when putting in the code for the tutorial.
It says that the lines with the problem are: 34, 39, 44, 49

using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
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

    public 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.5f);
    }

    void Spawn()
    {
        if (spawned == false)
        {

            if (openingDirection == 1)
            {
                rand = Random.Range(0, templates.bottomRooms.Length);
                Instantiate(templates.bottomRooms[rand], transform.position, templates.bottomRooms[rand].transform.rotation);
            }
            else if (openingDirection == 2)
            {
                rand = Random.Range(0, templates.topRooms.Length);
                Instantiate(templates.topRooms[rand], transform.position, templates.topRooms[rand].transform.rotation);
            }
            else if (openingDirection == 3)
            {
                rand = Random.Range(0, templates.leftRooms.Length);
                Instantiate(templates.leftRooms[rand], transform.position, templates.leftRooms[rand].transform.rotation);
            }
            else if (openingDirection == 4)
            {
                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.closedRooms, transform.position, Quaternion.identity);              
                Destroy(gameObject);
            }
            spawned = true;
        }
    }
}

Any help would be greatly appreciated!

Put a debug.log check if templates been assigned. If it is check all the templates.xxxxroom see is all of them is null

Where should I put debug.log? (Sorry I’m new to this kind of stuff lol)

Can put on start of spwan

If(templaes == null)
{
    Debug.Log("templates not been set");
}

If this been trigger and display and the console log then this haven’t found the object you were hoping for

templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();

And can do a debug.log for all the templates.xxxxroom

oooh ok thank you! I’ll try that. And if the ‘Templates’ aren’t null? How do I set that?

If is null . Why don’t you just drag it in from editor since your templates is public

If isn’t null make sure the object with the tag rooms have the script RoomTemplates

omg thank you so much! I finally found out that I put the script to the wrong Game Object. You’re a life saver, I hope you have a great day

1 Like

I still have this issue except only on line 62. I have the script on the correct gameobject and everything, I would love some help.