Question by
penguinsrepic · Apr 13, 2020 at 07:46 PM ·
c#2ddoorsdungeon
When all of the enemies in a room dies the doors dont open.
In the last section of this code the
else
{
foreach(Door door in room.GetComponentsInChildren<Door>())
{
always is zero and I can't figure out why. each cycle of the foreach loop above it decreases the door count by one but when the loop is exited it doesnt reset for some reason.
public void UpdateRooms()
{
foreach (Room room in loadedRooms)
{
if (currRoom != room)
{
EnemyController[] enemies = room.GetComponentsInChildren<EnemyController>();
if(enemies != null)
{
foreach(EnemyController enemy in enemies)
{
enemy.notInRoom = true;
aIDestination = enemy.gameObject.GetComponent<AIDestinationSetter>();
aIDestination.target = enemy.gameObject.transform;
}
foreach(Door door in room.GetComponentsInChildren<Door>())
{
door.doorCollider.SetActive(true);
}
}
else
{
foreach(Door door in room.GetComponentsInChildren<Door>())
{
door.doorCollider.SetActive(true);
}
}
}
else
{
EnemyController[] enemies = room.GetComponentsInChildren<EnemyController>();
if(enemies.Length > 0)
{
foreach(EnemyController enemy in enemies)
{
enemy.notInRoom = false;
aIDestination = enemy.gameObject.GetComponent<AIDestinationSetter>();
aIDestination.target = GameObject.FindWithTag("Player").transform;
}
foreach(Door door in room.GetComponentsInChildren<Door>())
{
Debug.Log(room + "a " + room.GetComponentsInChildren<Door>().Length);
door.doorCollider.SetActive(false);
Debug.Log(room + "b " + room.GetComponentsInChildren<Door>().Length);
}
Debug.Log(room + "c " + room.GetComponentsInChildren<Door>().Length);
}
else
{
foreach(Door door in room.GetComponentsInChildren<Door>())
{
door.doorCollider.SetActive(true);
}
}
}
}
}
Comment
I have the door.doorCollider set to the door and I think that might be why its not working but I dont see why that would remove the door from the script entirely
Your answer
Follow this Question
Related Questions
Need help with incorporating 2D dungeon algorithm into Unity. 1 Answer
C# 2D Top down game how to detect if objects are touching while they can pass through each other? 1 Answer
Trying to move UI Elements but they are not working 1 Answer
Error when adding an item to a list 0 Answers
position the player on the same position as an object 0 Answers