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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

SOLVED: Problem with random dungeon generator

Discussion in 'Scripting' started by ProExCurator, Jan 28, 2020.

  1. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    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:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RoomTemplates : MonoBehaviour
    6. {
    7.    public GameObject closedRoom;
    8.    public GameObject[] bottomRooms;
    9.    public GameObject[] topRooms;
    10.    public GameObject[] leftRooms;
    11.    public GameObject[] rightRooms;
    12. }
    RoomSpawner:
    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.     public int MaximumRooms;
    17.  
    18.     void Start(){
    19.      
    20.         templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();
    21.         Invoke("Spawn", 0.1f);
    22.     }
    23.  
    24.     void Spawn(){
    25.         if(spawned == false && MaximumRooms <= 10){
    26.             if(openingDirection == 1){
    27.                 // Need to spawn a room with a BOTTOM door.
    28.                 rand = Random.Range(0, templates.bottomRooms.Length-1);
    29.                 Instantiate(templates.bottomRooms[rand], transform.position, templates.bottomRooms[rand].transform.rotation);
    30.                 MaximumRooms++;
    31.             } else if(openingDirection == 2){
    32.                 // Need to spawn a room with a TOP door.
    33.                 rand = Random.Range(0, templates.topRooms.Length);
    34.                 Instantiate(templates.topRooms[rand], transform.position, templates.topRooms[rand].transform.rotation);
    35.                 MaximumRooms++;
    36.             } else if(openingDirection == 3){
    37.                 // Need to spawn a room with a LEFT door.
    38.                 rand = Random.Range(0, templates.leftRooms.Length);
    39.                 Instantiate(templates.leftRooms[rand], transform.position, templates.leftRooms[rand].transform.rotation);
    40.                 MaximumRooms++;
    41.             } else if(openingDirection == 4){
    42.                 // Need to spawn a room with a RIGHT door.
    43.                 rand = Random.Range(0, templates.rightRooms.Length);
    44.                 Instantiate(templates.rightRooms[rand], transform.position, templates.rightRooms[rand].transform.rotation);
    45.                 MaximumRooms++;
    46.             }
    47.             spawned = true;
    48.         }
    49.     }
    50.  
    51.     void OnTriggerEnter2D(Collider2D other){
    52.         if(other.CompareTag("Spawnpoint")){
    53.             if(other.GetComponent<RoomSpawner>().spawned == false && spawned == false){
    54.                 Instantiate(templates.closedRoom, transform.position, Quaternion.identity);
    55.                 Destroy(gameObject);
    56.             }
    57.             spawned = true;
    58.         }
    59.     }
    60. }





    I tried to limit number of spawned rooms by variable "MaximumRooms" but it doesn't work.

    Can someone help?
     
    Last edited: Jan 28, 2020
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    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.
     
  3. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    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
     
  4. diXime

    diXime

    Joined:
    Oct 2, 2018
    Posts:
    162
    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.
     
    ProExCurator likes this.
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    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.)
     
    ProExCurator likes this.
  6. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    Okay, I fixed it. The problem was with spawning points, they were too close and collisions... you know. Thanks for help anyway ;)

    How it looks now: https://i.imgur.com/ASKgRx0.mp4
     
  7. SaladCrab

    SaladCrab

    Joined:
    Mar 31, 2020
    Posts:
    1
    what does the code look like now?