- Home /
Is there some way to remove and destroy gameobject from list once?
When i remove and destory gameobject list. I just type like this.
List<GameObject> list = new List<GameObject>();
for (int i = list.Count-1; i>0; i--)
{
GameObject o = list[i];
cardBunch.RemoveAt(i);
// -> I think these 2 lines make 1 line.
}
I think some way to improve this. but i have no clue or something... so if you have any suggestion please tell me. thanks!
The code example you gave makes very little sense whatsoever. What is "cardBunch"? Why do you declare "GameObject o" when you don't use it? What does your question even mean? You want to destroy gameobject and remove it from a list with one line of code?
Short answer: not possible. Those are two separate operations and you have to write two instructions to achieve this effect.
If what you want is destroy the GameObject and remove it from the list then you need to add one line`Destroy(o)`
Answer by WarmedxMints · Mar 02, 2019 at 03:04 PM
You could just use a foreach loop
List<GameObject> MyList = new List<GameObject>();
foreach (var gameObject in MyList)
{
Destroy(gameObject);
}
MyList.Clear();
Your answer
Follow this Question
Related Questions
ERROR BCE0043 i need help pls 1 Answer
Editor calling a constructor for no apparent reason 1 Answer
Editor Script doesn't work on restart 1 Answer
Simplistic air/contamination flow between rooms 0 Answers
How can i make both two cameras to follow the player but only one with control on player ? 0 Answers