Reputation: 173
I got a quick question.
I have a List of objects that is regularly accessed by another script. What I want to do is clear the list and destroy all objects in it. Here is the code I am using to do this:
FoodTargets is the name of my List...
destroycounter = FoodTargets.Count;
for (destroycounter = 0; destroycounter < FoodTargets.Count; destroycounter++)
{
Destroy(FoodTargets[destroycounter]);
FoodTargets.RemoveAt(destroycounter);
}
This returns the error:
Can't destroy transform of "". if you want to destroy the game object, please call destroy on the game object instead...
I've been at this for hours, I just want to know what line of code I can use to destroy the instantiated prefab inside the list... OR if possible, what code do I use to destroy all instantiated prefabs in a List. Thanks in advance, your help will be much appreciated.
Upvotes: 1
Views: 1946
Reputation: 7824
Use the fact that each transform points to its GameObject. You have to do this because Destroy() takes a GameObject as a parameter.
Destroy(transformReference.gameObject);
In your code it would look like this:
Destroy(FoodTargets[destroycounter].gameObject);
You can read more about the properties inside the Transform here.
Upvotes: 4