- Home /
Rooms are infinitely spawning, don't know what's keeping the loop from ending
Most of the question is in the title, my loop for spawning room prefabs doesn't end and unity crashes, what's stopping the loop from ending? The error is somewhere in lines 26-52.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RoomSpawner : MonoBehaviour
{
public int openingDirection;
// 1 B
// 2 T
// 3 L
// 4 R
private 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.1f);
}
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 OnTriggerEnter3D(Collider2D other)
{
if (other.CompareTag("SpawnPoint"))
{
if(other.GetComponent<RoomSpawner>().spawned == false && spawned == false)
{
Instantiate(templates.closedRoom[rand], transform.position, Quaternion.identity);
Destroy(gameObject);
}
spawned = true;
}
}
}
Do any of the rooms generated have scripts that call back to this RoomSpawner script? For that matter, if all of them do, then they would potentially keep triggering new rooms to spawn before the first could ever allow for spawned = true;
to be called.
On an unrelated note, is OnTriggerEnter3D() a typo?
Yes they do, each prefab I made has this script applied to it along with being called on from this list
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RoomTemplates : $$anonymous$$onoBehaviour
{
public GameObject[] bottomRooms;
public GameObject[] topRooms;
public GameObject[] leftRooms;
public GameObject[] rightRooms;
public GameObject[] closedRoom;
public List<GameObject> rooms;
public float waitTime;
private bool spawnedBoss;
public GameObject boss;
void Update()
{
if(waitTime <= 0 && spawnedBoss == false)
{
for (int i = 0; i < rooms.Count; i++)
{
if(i == rooms.Count - 1)
{
Instantiate(boss, rooms[i].transform.position, Quaternion.identity);
spawnedBoss = true;
}
}
}
else
{
waitTime -= Time.deltaTime;
}
}
}
And to answer your other question no it is not a typo, but I don't know how to do 3D collision detection, so I just tried using OnTriggerEnter2D but with 3 dimensions and it didn't give me an error so I left it
Okay, starting back from the top, let's see what this is doing based on the scripts alone:
The RoomSpawner finds an existing RoomTemplates in your scene, then generates a single room from it.
Then, the RoomTemplates cycles through its "rooms" List<>, waiting until the last entry to spawn a boss in the room.
That's it.
What is the context for the RoomSpawner, then? Are RoomSpawners also being generated by something? There's nothing designating the "openingDirection" in them, so is that being set at an even higher level?
I point out OnTriggerEnter3D() because Unity won't inherently call that function. Based on you using Collider2D in it, I'm assu$$anonymous$$g your game is 2D and, by extension, running 2D physics. Do note, however, that should not expect 2D physics objects to be able to interact with 3D physics objects.
On that note, OnTriggerEnter(Collider) (3D) and OnTriggerEnter2D(Collider2D) (2D) are examples of functions that Unity will call where applicable. If your function doesn't use a predefined name (any of the "$$anonymous$$essages" functions), it will just be an ordinary function for you to call on your own.
Answer by CmdrZin · Jan 19, 2021 at 11:54 PM
As a guess, you might be instantiating the new room on top of this object and colliding with it. That would trigger the OnTriggerEnter3D() to spawn another room which would then trigger the OnTriggerEnter3D() again. It's the only possible 'loop' I see.
Try offsetting the transform.position for the closedRoom.
Good luck.
Another thought. Try this.SetActive(false) to disable the this Collider before Instantiating the new room.
This seems to be partially the case, the rooms all spawn on top of each other (literally) and begin to branch out into a tree like shape.
Additionally upon trying this I have a new error: 'RoomSpawner' does not contain a definition for 'SetActive' and no accessible extension method 'SetActive' accepting a first argument of type 'RoomSpawner' could be found
Setactive() is a GameObject method.
Use this.gameObject.SetActive(false);
Your answer
![](https://koobas.hobune.stream/wayback/20220613023622im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do i stop infinite Jumping in the air 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Dissolve shader script little proplemo, help me 0 Answers
Global Fog Messy Transition Between Objects and Skybox 1 Answer