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. Dismiss Notice

Question I'm currently working on a procedurally generated game and the game doesn't stop spawning rooms

Discussion in 'Scripting' started by PanicMedia, Jan 3, 2021.

  1. PanicMedia

    PanicMedia

    Joined:
    Dec 6, 2020
    Posts:
    37
    My game has procedural generation in it and for some reason it doesn't stop spawning in rooms and ends up spawning enough rooms to freeze unity. If anyone has any suggestions on how to limit the amount of rooms that my script can generate that would be very appreciated. Here is the code I used.

    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.  
    17.     /// <summary>
    18.     /// Start is called on the frame when a script is enabled just before
    19.     /// any of the Update methods is called the first time.
    20.     /// </summary>
    21.     void Start()
    22.     {
    23.         templates = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomTemplates>();
    24.         Invoke("Spawn", 0.1f);
    25.     }
    26.  
    27.     /// <summary>
    28.     /// Update is called every frame, if the MonoBehaviour is enabled.
    29.     /// </summary>
    30.     void Spawn()
    31.     {   if(spawned == false)
    32.     {
    33.         if(openingDirection == 1)
    34.         {
    35.             //Need  to spawn a room with a BOTTOM door
    36.             rand = Random.Range(0, templates.bottomRooms.Length);
    37.             Instantiate(templates.bottomRooms[rand], transform.position, templates.bottomRooms[rand].transform.rotation);
    38.         } else if(openingDirection == 2)
    39.         {
    40.             // Need to spawn a room with a TOP door
    41.             rand = Random.Range(0, templates.topRooms.Length);
    42.             Instantiate(templates.topRooms[rand], transform.position, templates.topRooms[rand].transform.rotation);
    43.         } else if(openingDirection == 3)
    44.         {
    45.             // Need to spawn a room with a LEFT door
    46.             rand = Random.Range(0, templates.leftRooms.Length);
    47.             Instantiate(templates.leftRooms[rand], transform.position, templates.leftRooms[rand].transform.rotation);
    48.         } else if(openingDirection == 4)
    49.         {
    50.             // Need to spawn a room with a RIGHT door.
    51.             rand = Random.Range(0, templates.rightRooms.Length);
    52.             Instantiate(templates.rightRooms[rand], transform.position, templates.rightRooms[rand].transform.rotation);
    53.         }
    54.         }
    55.         spawned = true;
    56.        
    57.     }
    58.  
    59.    /// <summary>
    60.     /// OnTriggerEnter is called when the Collider other enters the trigger.
    61.     /// </summary>
    62.     /// <param name="other">The other Collider involved in this collision.</param>
    63.     void OnTriggerEnter(Collider other)
    64.     {
    65.         if(other.CompareTag("SpawnPoint") && other.GetComponent<RoomSpawner>().spawned == true)
    66.         {
    67.             Destroy(gameObject);
    68.         }
    69.     }
    70. }
     
  2. Ardenian

    Ardenian

    Joined:
    Dec 7, 2016
    Posts:
    313
    Does any of your room templates have a
    RoomSpawner
    component? Does the GameObject that has the
    RoomSpawner
    component also has a
    RoomTemplates
    component? If any of these two are true, then you would spawn a new room spawner in your spawner which then would spawn more rooms and spawners and so on.

    I highly recommend you to look into Prefabs for your room templates. Prefabs are templates themselves, but scene-independent and you don't have to awkwardly look for them in your scene, but can reference and instantiate the prefabs instead. It gives you a lot more control over your system.
     
    PraetorBlue likes this.
  3. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    Something else is going on besides this script, as this should only spawn one room.
    Check the hierarchy: do you see "(Clone) (Clone)" etc? If there's more than one "(Clone)" after a spawned object, that means there's recursion as Ardenian mentioned (a spawned object has a function to spawn itself). Everything should be spawned from one master script only, and they should be prefabs. Hope that helps.
     
  4. PanicMedia

    PanicMedia

    Joined:
    Dec 6, 2020
    Posts:
    37
    the spawn points don't have a room template script
     
  5. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    We need more info. Somewhere there likely is recursion, so try removing all scripts from all game objects, then put them back. You might have an extra one somewhere...?