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...
不能破坏“”的转换。如果您想要销毁游戏对象,请在游戏对象上调用destroy。
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.
我已经讲了好几个小时了,我只想知道我可以用什么代码来销毁列表中实例化的预置。或者,如果可能,我使用什么代码来销毁列表中的所有实例化的prefabs。感谢您的帮助,我们将不胜感激。
1 个解决方案
#1
4
Use the fact that each transform points to its GameObject. You have to do this because Destroy() takes a GameObject as a parameter.
使用每个转换指向其游戏对象的事实。您必须这样做,因为Destroy()将一个GameObject作为参数。
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.
您可以阅读更多关于这个转换的属性。
#1
4
Use the fact that each transform points to its GameObject. You have to do this because Destroy() takes a GameObject as a parameter.
使用每个转换指向其游戏对象的事实。您必须这样做,因为Destroy()将一个GameObject作为参数。
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.
您可以阅读更多关于这个转换的属性。