- Home /
Invalid Cast Exception
I can't find the solution. What do I do wrong?
var tegenstanders : List.<GameObject> = new List.<GameObject>();
for(i in tegenstanders)
{
Destroy(tegenstanders[i].gameObject);
}
Answer by Mike 3 · May 16, 2011 at 01:24 PM
In your code above, i is already the object you want to destroy the GameObject of, but you're then trying to use it as an index into the list
Basically just use i instead, and probably name i better so it's a little more obvious (i is generally used for the index, not an object from the collection)
Way better ;) But you don't have to use item.gameObject
since item
is of type GameObject. Destroy(item);
should be enough.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Find specific prefab in a list 2 Answers
how to know when list is empty? 3 Answers
How To Get A Reference To All Nearby GameObjects? 1 Answer
Adding a prefab to gameObject at a certain position in runtime 0 Answers