SOLVED: Problem with random dungeon generator

Guys, I have problem with script for random dungeon generator. I did as it is in the videos on youtube tutorial.

but I have a problem that rooms are spawning infinite (sometimes they don’t, depends) and some people were saying in comments that they have the same issue, but none of the solutions for that were helpful. So I’m asking you, where is the problem?

The script looks like that:

RoomTemplates:

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

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

RoomSpawner:

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 int MaximumRooms;
 
    void Start(){
     
        templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();
        Invoke("Spawn", 0.1f);
    }
 
    void Spawn(){
        if(spawned == false && MaximumRooms <= 10){
            if(openingDirection == 1){
                // Need to spawn a room with a BOTTOM door.
                rand = Random.Range(0, templates.bottomRooms.Length-1);
                Instantiate(templates.bottomRooms[rand], transform.position, templates.bottomRooms[rand].transform.rotation);
                MaximumRooms++;
            } 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);
                MaximumRooms++;
            } 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);
                MaximumRooms++;
            } 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);
                MaximumRooms++;
            }
            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;
        }
    }
}




I tried to limit number of spawned rooms by variable “MaximumRooms” but it doesn’t work.

Can someone help?

The percentage of people on this forum who are familiar with any given third-party tutorial you found on the Internet is not likely to be high. If you can’t phrase your question in a way that requires no prior knowledge of this specific tutorial, then you should probably ask in some location connected with the tutorial.

I don’t understand what do you mean. I posted a whole script and the rest as pictures and I explained and showed how the problem looks like. What more can I do to be more specific? Explain please.

The script is very short so… and before I asked here I was looking there for help, but as I said the solutions weren’t working

Hello,
I have several issues with that code, the chain else if could be done cleaner with a switch statement and coroutines may be better than Invoke to load in a healthier way, but this is probably not linked to your problem.

In what situations does the room spawn this way? When you are switching rooms? Because your collider does look weird. Are you spawning a closed room everytime you are colliding with the SpawnRoom GameObject? This can happen a lot.

1 Like

First off: By itself, the script that you posted will never spawn more than one room, because the Spawn() function is only called one time. Either you have omitted an important script, or there is some key structural information in how your game objects are set up that you have not explained.

But even if you posted your entire Unity project and everything in it, that is not an explanation. You would be asking people to reverse-engineer your project’s intended functionality based on a broken implementation. That’s a lot of work!

A good debugging thread should start with a succinct but clear explanation of what result you are trying to get, your strategy for getting that result, and how the actual result differed from your expectation. This explanation should be understandable to someone who has never seen your game OR the tutorial that you are trying to follow. (For example, “I’m trying to limit how many rooms spawn” isn’t a good explanation of your goal, because it assumes the reader already knows what a room is and what process causes them to spawn.)

1 Like

Okay, I fixed it. The problem was with spawning points, they were too close and collisions… you know. Thanks for help anyway :wink:

How it looks now: Imgur: The magic of the Internet

what does the code look like now?