I am spawning a bunch of objects that have a tag assigned to it. I want to randomly destroy a certain amount of them [1,2,or 3] at a time at button press.
I have the objects added to an array from start.
I am spawning a bunch of objects that have a tag assigned to it. I want to randomly destroy a certain amount of them [1,2,or 3] at a time at button press.
I have the objects added to an array from start.
if the array is storing game objects you could just do something like this when the button is pressed:
void destroyObjects()
{
int numberOfObjectsToDestroy = Random.Range(1,3);
int i = 0;
while( i < numberOfObjectsToDestroy )
{
objectToDestroy = arrayOfObjects[Random.Range(0 , arrayOfObjects.length - 1)];
if(objectToDestroy.tag == "tag")
{
destroy(objectToDestroy);
i++;
}
}
}
that would choose random objects out of the array that have the tag and destroy them