【发布时间】:2017-03-24 14:11:42
【问题描述】:
我想在 Unity 中删除一个实例化的游戏对象。我在实例化一个动态按钮时使用了一个 for 循环,我想在再次使用它之前销毁它,因为按钮的数量只是附加而不是过度写入。这是我的实例化代码:
for(int i = 0; i < num; i++)
{
goButton = (GameObject)Instantiate(prefabButton);
goButton.transform.SetParent(panel, false);
goButton.transform.localScale = new Vector3(1, 1, 1);
//print name in Button
goButton.GetComponentInChildren<Text> ().text = names[i];
//get Url from firebase
string Url = url[i];
WWW www = new WWW (Url);
yield return www;
Texture2D texture = www.texture;
//load image in button
Image img = goButton.GetComponent<Image>();
img.sprite = Sprite.Create (texture, new Rect (0, 0, texture.width, texture.height),Vector2.zero);
Button tempButton = goButton.GetComponent<Button>();
int tempInt = i;
string name = names [i];
tempButton.onClick.AddListener (() => hello (tempInt, name));
}
我尝试在 for 循环之前添加 Destroy(goButton),但只有最后一个按钮被销毁。可能的解决方案是什么?谢谢
【问题讨论】:
标签: android unity3d destroy gameobject