Get one random object out of more objects with the same tag

i have this

  IEnumerator ShowExitNow()
   {
     
       GameObject[] obstacles = GameObject.FindGameObjectsWithTag("Exit");
    
       foreach (GameObject obst in obstacles)
       {
           obst.GetComponent<ExitTile>().ActivateMeNow(); 
           yield return null;
       }
   }

Now i get that this is taking all the Exit tag objects and activate their functions. How to make it pick one out of all the Exit tagged objects. A example would be great.

1 Like

You have an array, and you have a count of objects.

So it will be something like that:

if (obstacles != null)
{
    GameObject randomObject = obstacles[Random.Range(0, obstacles.Length)];
}
3 Likes

Thank you so much i did not understand the logic but now i do :slight_smile:
Thank you for taking the time to help out.

1 Like