I want destroy all objects with name “food(clone)” when i click Escape
but it destroyed only one of that …
Destroy(GameObject.Find("food(clone)"));
Have the game object created with a tag. Then in your code create an array of game objects using = GameObject.FindGameObjectsWithTag. Then have a foreach loop that destroys all members of that array.
ie.
GameObject[] foods;
foods = GameObject.FindGameObjectsWithTag("Food");
foreach(GameObject food in foods)
{
Destroy(food);
}
i think it would be better for you to keep a reference of the object you instantiated, maybe in a list. and when you want to destroy them, you just loop into your list and destroy everything then clear your list.
Something like this
List<gameobject> allTheFood = new List<gameobject>();
private void CreateFood()
{
for(int i = 0; i < 10; i++)
{
allTheFood.add(Instantiate(foodprefab) as gameobject;);
}
}
private void DestroyFood()
{
for(int i = 0; i < allTheFood.count; i++)
{
Destroy(allTheFood*);*
}
allTheFood.clear();
}
OLGV
3
@TheDJBuntin doesn’t seem to work on mobile :
,though it works on PC