【问题标题】:Can't destroy transform of "". if you want to destroy the game object, please call destroy on the game object instead不能破坏“”的变换。如果你想销毁游戏对象,请在游戏对象上调用destroy
【发布时间】:2014-05-22 10:13:49
【问题描述】:

我有个小问题。

我有一个由另一个脚本定期访问的对象列表。我想要做的是清除列表并销毁其中的所有对象。这是我用来执行此操作的代码:

FoodTargets 是我列表的名称...

    destroycounter = FoodTargets.Count;

    for (destroycounter = 0; destroycounter < FoodTargets.Count; destroycounter++)
    {
        Destroy(FoodTargets[destroycounter]);
        FoodTargets.RemoveAt(destroycounter);
    }

这会返回错误:

不能破坏“”的变换。如果你想销毁游戏对象, 请改为在游戏对象上调用destroy...

我已经在这几个小时了,我只是想知道我可以使用哪一行代码来销毁列表中的实例化预制件......或者如果可能的话,我使用什么代码来销毁所有实例化的预制件一个列表。在此先感谢您的帮助,我们将不胜感激。

【问题讨论】:

  • 你可以试试:Destroy(FoodTargets);没有 for 循环?
  • 我试过了。但它返回错误:Assets/Scrpits/HerbivoreScript.cs(92,17): error CS1503: Argument #1' cannot convert System.Collections.Generic.List' expression to type `UnityEngine.Object'跨度>
  • @Max 不,这在 Unity3D 中不起作用。

标签: c# unity3d destroy gameobject


【解决方案1】:

利用每个变换都指向其游戏对象这一事实。您必须这样做,因为Destroy()GameObject 作为参数。

Destroy(transformReference.gameObject);

在您的代码中,它看起来像这样

Destroy(FoodTargets[destroycounter].gameObject);

您可以阅读更多关于Transform here 中的属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    相关资源
    最近更新 更多