Hi, i am trying to make a procedurally generating room system. All the rooms have a BoxCollider3d, that acts as a trigger, and a script attached to them. For some reason, when i run into 2 colliders at the same time, the Generate function gets called twice, even tho i have a boolean that prevents this from happening. (sorry for my english, its my 2nd language)
Here is the c# script attached to all of the rooms:
using System.Collections.Generic;
using UnityEngine;
public class Level1MainRoomChunk : MonoBehaviour
{
public List<GameObject> Objects;
[SerializeField]
public Level1MainGridConnectionPointType[] GridConnectionPoints = new Level1MainGridConnectionPointType[4];
public GameObject[] NeighbourRooms = new GameObject[4]; // Clockwise from North
private Level1MainMapGenerator mainRef;
public bool IsSpawningNeighbours;
public void OnTriggerEnter(Collider other)
{
if (other.GetComponent<CharacterMovement>())
{
foreach (GameObject _object in Objects)
{
if(_object.GetComponent<MeshRenderer>() != null) _object.GetComponent<MeshRenderer>().enabled = true;
if(_object.GetComponent<Light>() != null)_object.GetComponent<Light>().enabled = true;
if(_object.GetComponent<BoxCollider>() != null)_object.GetComponent<BoxCollider>().enabled = true;
}
if (NeighbourRooms.Length == 0)
{
NeighbourRooms = new GameObject[] { null, null, null, null };
}
if(NeighboursGenerating() == false)GenerateAllNeighbouringRooms();
}
}
public void OnTriggerExit(Collider other)
{
if (other.GetComponent<CharacterMovement>())
{
foreach (GameObject _object in Objects)
{
if(_object.GetComponent<MeshRenderer>() != null) _object.GetComponent<MeshRenderer>().enabled = false;
if(_object.GetComponent<Light>() != null) _object.GetComponent<Light>().enabled = false;
if(_object.GetComponent<BoxCollider>() != null) _object.GetComponent<BoxCollider>().enabled = false;
}
}
}
public void setMainRef(Level1MainMapGenerator refer)
{
mainRef = refer;
}
public void GenerateAllNeighbouringRooms()
{
print("Generate");
if (mainRef != null)
{
for (int i = 0; i < NeighbourRooms.Length; i++)
{
print(NeighbourRooms[i]);
if (NeighbourRooms[i] == null)
{
if (GridConnectionPoints[i] != Level1MainGridConnectionPointType.NoSpawn)
{
IsSpawningNeighbours = true;
NeighbourRooms[i] = i switch
{
0 =>
// Top
mainRef.SpawnRandomRoomAt(transform.position + new Vector3(0, 0, 20),
Quaternion.identity, GridConnectionPoints[i], 2, this),
1 =>
//Right
mainRef.SpawnRandomRoomAt(transform.position + new Vector3(20, 0, 0),
Quaternion.identity, GridConnectionPoints[i], 3, this),
2 =>
//Bottom
mainRef.SpawnRandomRoomAt(transform.position + new Vector3(0, 0, -20),
Quaternion.identity, GridConnectionPoints[i], 0, this),
3 =>
//Left
mainRef.SpawnRandomRoomAt(transform.position + new Vector3(-20, 0, 0),
Quaternion.identity, GridConnectionPoints[i], 1, this),
_ => NeighbourRooms[i]
};
}
}
}
}
}
private bool NeighboursGenerating()
{
foreach (GameObject neighbour in NeighbourRooms)
{
if (neighbour != null)
{
if (neighbour.GetComponent<Level1MainRoomChunk>().IsSpawningNeighbours)
{
return true;
}
}
}
return false;
}
}
public enum Level1MainGridConnectionPointType
{
Empty,
Wall,
NoSpawn
}
Here is the code of the map generator:
using System.Collections.Generic;
using UnityEngine;
public class Level1MainMapGenerator : MonoBehaviour
{
[SerializeField] private List<GameObject> _prefabs;
[SerializeField] private GameObject StartRoom;
// Start is called before the first frame update
void Start()
{
GameObject firstRoom = Instantiate(StartRoom, Vector3.zero, Quaternion.identity);
firstRoom.GetComponent<Level1MainRoomChunk>().setMainRef(this);
if (firstRoom.GetComponent<Level1MainRoomChunk>().NeighbourRooms.Length == 0)
{
firstRoom.GetComponent<Level1MainRoomChunk>().NeighbourRooms = new GameObject[] { null, null, null, null };
}
}
// Update is called once per frame
void Update()
{
}
public GameObject SpawnRandomRoomAt(Vector3 pos, Quaternion rot, Level1MainGridConnectionPointType conType,
int conId, Level1MainRoomChunk refer)
{
if (GetRightRooms(conId, conType).Count == 0)
{
print("Cant find new room");
return null;
}
int RoomId = Random.Range(0, GetRightRooms(conId, conType).Count - 1);
print("SpawnRandomRoomAt");
GameObject ret = Instantiate(GetRightRooms(conId, conType)[RoomId], pos, rot);
ret.GetComponent<Level1MainRoomChunk>().setMainRef(this);
if (ret.GetComponent<Level1MainRoomChunk>().NeighbourRooms.Length == 0)
{
ret.GetComponent<Level1MainRoomChunk>().NeighbourRooms = new GameObject[] { null, null, null, null };
}
ret.GetComponent<Level1MainRoomChunk>().NeighbourRooms[conId] = refer.gameObject;
print(ret.GetComponent<Level1MainRoomChunk>().NeighbourRooms[conId]);
refer.IsSpawningNeighbours = false;
return ret;
}
public List<GameObject> GetRightRooms(int conId, Level1MainGridConnectionPointType conType)
{
List<GameObject> ret = new List<GameObject>();
foreach (GameObject room in _prefabs)
{
if (room.GetComponent<Level1MainRoomChunk>().GridConnectionPoints[conId] == conType)
{
ret.Add(room);
}
}
return ret;
}
}