- Home /
Remove object in a list with criteria
HI, I'm trying to remove an object from a list and disable its components. On event I'm trying to find an object in a list and then remove all components of it
void releaseThrow()
{
if Queue.Count < 1)
{
Debug.Log("No objects in queue");
}
else
{
foreach (GameObject objct in Queue)
{
if (objct.GetComponent<movementNEW>().thrown == true) //turns itself off after everything
{
objct.GetComponent<PossessionThrow>().enabled = false;
Queue.Remove(objct);
return;
}
}
}
}
This code sometimes works but I often find it randomly removing multiple objects in my already created list.
Any ideas?
As a side note - I only really need to iterate the list once, that's why the return. Couldn't figure out a way to do this without a foreach.
Answer by Bunny83 · Nov 10, 2021 at 09:32 AM
Sorry but your problem is not clear to me. I don't quite understand what
it randomly removing multiple objects in my already created list.
This does not make much sense to me. If you execute this method once it will at most remove a single element from the "Queue" since you return after that. So unless you call this method several times there is no way that this code removes more than one object.
In your comment you said
I only really need to iterate the list once,
Though if an object is found that matches your condition you stop the loop, so it may not iterate through the whole list if it finds a matching element somewhere in the middle.
If you actually want to remove all objects where "thrown" is set to true, you can not use a foreach loop as you can not modify a collection while iterating over it. I don't know what "Queue" is. A better name would help with the readability of your code. There is a Queue class which makes things extremely confusing. Though I guess it's a List<GameObject>
? If that's the case and you want to remove all objects that have their "thrown" field set to true, you can do:
for(int i = Queue.Count-1; i>=0; i--)
{
var movementComp = Queue[i].GetComponent<movementNEW>();
if (movementComp && movementComp.thrown)
{
Queue[i].GetComponent<PossessionThrow>().enabled = false;
Queue[i].RemoveAt(i);
}
}
Note that it's necessary to iterate through the list backwards as removing elements from the beginning would mess up the indices of the following elements. By iterating backwards, only the indices of already processed elements would change. Also I don't know if all the objects in that List actually contains those two components. If not you would get an exception. Though I guess you would have told us that, right?
In case you don't want to remove all objects where "thrown" is set to true but only the first one, your original code would do that. If something does not work right, the issue has to be somewhere else. Maybe you have a similar issue to this recent question?
I believe I fixed my issue, My code is really spaghettified but It all had to do with the:
movementNEW>().thrown bool not being reset, The issue then seemed to just clear the whole list made.
Essentially I have a list in which objects are pushed into it to activate, but then other objects (the thrown ones) only stay on the top of the stack and behave differently when activated. Not all objects in the list contain the same things they all do contain movement to activate though.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Find GameObjects with a certain Script [Solved] 1 Answer
print the whole list to screen 1 Answer
Find the differences between two lists 1 Answer
Show and Edit Attributes of my list of structs in EditorWindow. 0 Answers