[英]Destroying instatiated objects in unity
I have 2 different scripts attached to 2 different game objects. 我将2个不同的脚本附加到2个不同的游戏对象上。
One instantiates a bunch of game obejcts and I want the second one to destroy them. 一个实例化了一堆游戏对象,我希望第二个实例销毁它们。
code1:
public static volatile GameObject newObj;
public GameObject go;
newObj = GameObject.Instantiate(go);
newObj.tag = "mytag";
Code 2:
GameObject[] all_cloned_prefabs = GameObject.FindGameObjectsWithTag("mytag");
foreach (var AllPrefabs in all_cloned_prefabs)
{
Destroy(AllPrefabs);
}
When I run code 2 (let's say by pressing a button on GUI, it doesn't destroy the objects I created on the screen.What am I doing wrong? 当我运行代码2时(例如通过在GUI上按下按钮,它不会破坏我在屏幕上创建的对象。我在做什么错?
It's hard to understand what you are trying to do in the first code with public static volatile GameObject newObj;
使用
public static volatile GameObject newObj;
很难理解您在第一个代码中要做什么public static volatile GameObject newObj;
, it's not used in the other part of the code you shown. ,在您显示的代码的其他部分中未使用它。
The second code looks fine, what could be wrong is: 第二个代码看起来不错,可能是错误的:
FindGameObjectsWithTag
will not return inactive GameObjects). FindGameObjectsWithTag
将不会返回非活动的FindGameObjectsWithTag
)。 Try to Debug.Log the content of the all_cloned_prefabs
array to see if it actually contains anything. 尝试调试 。记录
all_cloned_prefabs
数组的内容以查看其是否实际包含任何内容。
I put all the objects inside a list, made the list public static volatile prefab1
. 我将所有对象放入列表中,使列表成为
public static volatile prefab1
。 accessed it from second script. 从第二个脚本访问了它。 then destroyed them It worked perfect.
然后销毁它们,效果很好。
foreach (GameObject obj1 in script1.prefab1)
{
Destroy(obj1);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.