2014-05-22 31 views
1

我有一個快速的問題。不能破壞「」的變形。如果你想摧毀遊戲對象,請調用摧毀遊戲對象而不是

我有一個由另一個腳本定期訪問的對象列表。我想要做的是清除列表並銷燬其中的所有對象。下面是我使用這樣做代碼:

FoodTargets是我的列表的名稱...

destroycounter = FoodTargets.Count; 

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

這將返回錯誤:

Can't destroy transform of "". if you want to destroy the game object, please call destroy on the game object instead...

我一直在這幾個小時,我只想知道我可以使用哪行代碼來銷燬列表中的實例化預製...或者,如果可能,我使用什麼代碼來銷燬列表中的所有實例化預製。在此先感謝,您的幫助將不勝感激。

+0

你可以試試:'Destroy(FoodTargets)';沒有for循環? – Max

+0

我試過了。但它返回錯誤:Assets/Scrpits/HerbivoreScript.cs(92,17):錯誤CS1503:參數'#1'無法將'System.Collections.Generic.List '表達式轉換爲鍵入'UnityEngine。對象' – SandyBites

+0

@Max不,在Unity3D中不起作用。 – FunctionR

回答

4

使用每個變換指向其GameObject的事實。你必須這樣做,因爲Destroy()需要一個GameObject作爲參數。

Destroy(transformReference.gameObject); 

在你的代碼,它看起來像這樣

Destroy(FoodTargets[destroycounter].gameObject); 

你可以閱讀更多關於Transform here裏面的屬性。

+0

謝謝,這個工作非常好。 – SandyBites