- Home /
Removing GameObjects from list (with List.Remove()) doesn't seem to be removing them
EDIT: Someone on a different forum told me to add if(!roomsInProg.Contains(currentRoom)) roomsInProg.Add(currentRoom);
to my code, that fixed it, apparently i had duplicates on my list.
So i'm working on a dungeon generator in Unity and in my script I'm adding the rooms i generate to a list of rooms that are in progress, then remove it when its done generating and start generating the next one on the list.
here i'm setting the current room
currentRoom = Instantiate(roomPrefabs[Random.Range(0, roomPrefabs.Length)], new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
then here i'm adding this room to the list of rooms in progress
roomsInProg.Add(currentRoom);
here is an example of me adding a new room that i spawned in to the list
if (roomScript.connectionsAvailable[0] == true && roomScript.rCheck[0].room == null && n<roomCount) {
tempRoom = Instantiate(roomPrefabs[Random.Range(0, roomPrefabs.Length)], new Vector3(0, roomDistanceY, 0) + currentRoom.transform.position, Quaternion.identity) as GameObject;
roomsInProg.Add(tempRoom);
tempRoom.GetComponent<RoomScript>().directionFromSpawn = 0;
n++;
if (n>=roomCount) {
firstOver = true;
}
}
and here i am removing the current room from the list
if (!firstOver) {
roomsInProg.Remove(currentRoom);
currentRoom = roomsInProg[0];
}
if (n<roomCount || roomsInProg.Count > 0) {
Invoke("SpawnRoom", 0.01f);
}
}
Here is the entire code, if necessary https://pastebin.com/C9TKnQPL Other Scripts if necessary RoomScript pastebin.com/HPSFJ7RB roomCheck pastebin.com/umNvj9VG SpawnCheck pastebin.com/xkT7QtSJ
To clarify your question. Is the room not removed from the list or not removed from the scene? To remove from scene you have to destroy the gameobject or at least set it inactive.
It doesn't get removed from the list, I need it to stay in the scene.
perfect ti$$anonymous$$g, i just cleaned that up. I had that in because i was testing a workaround, i originally had only a list, but that didn't work, so i added this that i found on some post and forgot to change it back to the original, sorry for the confusion. I changed it back to only 1 list but it still does not work :/
I am a bit confused by the way you are using the list.
You are creating an array, roomsInProg from the list, then using that array to reinitialize th elist each time you try to add or remove a room from the list.
Is there a reason you are doing it this way? All that data back and forth between the list and the array just gives more room for errors, in my opinion. Unless you need that array elsewhere, I would eli$$anonymous$$ate it entirely. If you do need it elsewhere, I would create it only when needed, and discard it afterwards.
I would initialize the list once, at the start, then add and remove as necessary.
I tried to recreate your code, but without the RoomScript and SpawnCheck classes, I can't get it to a point I can test.
Entirely up to you, but if you want to provide copies of these classes, I am more than willing to go through it and see if I can get it working for you.
-Larry
Thank you, as i mentioned in a different comment that whole system with the arrays was a thing i put in as an attempt to fix it, it didn't work and i had forgotten about that, someone pointed out the mistake and i cleaned up the code to what it was originally with only 1 list. That did not fix the issue, someone on a different forum suggested that i use a Queue that didn't work either.
Ok a person on a different forum told me that i should add a check in to see if the room is already on the list and that actually seemed to have fixed that problem, although now i have some problems with rooms overlapping, maybe that was already there before i just didn't notice. Anyways thanks for the help!
Ok, I am trying to recreate your project now, to see if I can figure out how to get this working for you. I will let you know as soon as I have something.
Answer by JustARoom · Dec 06, 2019 at 04:49 PM
solved by adding if(!roomsInProg.Contains(currentRoom)) roomsInProg.Add(currentRoom);
when adding the current room and if(!roomsInProg.Contains(tempRoom)) roomsInProg.Add(temoRoom);
when adding the new rooms.
Answer by harsh2580 · Dec 06, 2019 at 03:54 PM
try to put a debug log next to your removing code and see if its get executed or not if not then logic not working
it does get executed, i tried doing it with queue and then Dequeue() but it didnt work either
Your answer
Follow this Question
Related Questions
How to add and remove objects from a list when they get destroyed and instantiated 0 Answers
How to store, change and delete gameobject and it's list element? 0 Answers
Removing a class object from a list(.remove not working) C# 1 Answer
How can I remove an item ffrom a list. 1 Answer
Unknown remaining objects after removing and destroying from list and parent game objects 0 Answers