There are 6 disable gameobjects.
I want when I click on a button ,one of six gameObjects actives randomly and if I click on the button again
it actives one of remaning gameObejects that are not active.
and after 6 clicks all my 6 gameObjects should be active.
can anyone help me ?
I put gameObjects in an array
GameObject list;
public GameObject GetRandomGameObject()
{
var groups = list.GroupBy(m => m.activeInHierarchy); //Created 2 groups, 1 contains enabled gameobjects, 1 contains disabled gameobjects
foreach (var group in groups)
{
if (group.Key)
continue;
List<GameObject> listObj = group.ToList();
int randomIndex = Random.Range(0, listObj.Count);
return listObj[randomIndex];
}
return null;
}
You can then use:
GetRandomGameObject().SetActive(false);