1

Just wondering if anyone could help me out with a small issue in my code. On line 60, there is an error essentially telling me what I am referencing to does not exist. To my knowledge it does, but I am very new to Unity. I am trying to create a random dungeon generator for a University project. My classes are below:

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
    //So for a room with a door on the right, you will type 3, as a left door is needed in the next room to connect the two rooms. 

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


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

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

    }

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

            }
            spawned = true;
        }
    }
}
using System.Collections.Generic;
using UnityEngine;

public class Destroyer : MonoBehaviour
{

    void OnTriggerEnter2D(Collider2D other)
    {
        Destroy(other.gameObject);

    }
}

This issue is causing the entry room to be blocked by closed rooms, which are meant to block exits out into the scene view. I have followed BlackThornProd's tutorial on this, but cannot figure out what I have done wrong. Here is a link to the tutorial. Many thanks!

EDIT Here I have included the RoomTemplates Class if it helps. Many Thanks!

using System.Collections.Generic;
using UnityEngine;

public class RoomTemplates : MonoBehaviour
{
    public GameObject[] bottomRooms;
    public GameObject[] topRooms;
    public GameObject[] leftRooms;
    public GameObject[] rightRooms;

    public GameObject closedRoom;
}
RhysGP15
  • 11
  • 3
  • 1
    and which line is actually causing the error?? – BugFinder Apr 25 '20 at 13:53
  • 2
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – BugFinder Apr 25 '20 at 13:53
  • The error is on line 60. To be clear - the code runs fine with no errors, but the Unity gives me an error when the game is played. Many Thanks. – RhysGP15 Apr 26 '20 at 11:59
  • OK well I aint counting your code lines... The issue is very clear. the code doesnt run fine if during runtime and played it throws an error, these are called runtime errors eg your code has not allowed for some element. So. if "other.GetComponent().spawned.." is giving an error the other doesnt have a RoomSpawner on it. How more clear can it be – BugFinder Apr 26 '20 at 12:13
  • @RhysGP15 We can't see which line is line 60, therefore it is difficult for someone to find the error and help you. You could specify which part of the code gives you an error or add a comment to your code where the error is, so others can find it easily and help you. Welcome to Stack Overflow! – Periklis Vai Apr 26 '20 at 13:22

1 Answers1

0

Are templates a gameobject? If so, I think you can try something like this:

private GameObject templates;

void Start()
    {
        templates = GameObject.FindGameObjectWithTag("Rooms");
        RoomTemplates Templates = templates.GetComponent<RoomTemplates>();
        Invoke("Spawn", 0.1f);
    }
  • Templates is another class which stores arrays of rooms. I have edited the post to include this class if it helps resolve the issue. Many Thanks. – RhysGP15 Apr 26 '20 at 12:05