Selecting Game Object From A Selection At Random

Hey guys I am trying to create a side scrolling game with uses different Pre created sections at random. All of my game movement is done by the world so the camera and player are static. What I am current having problems with is getting my different zones to be selected at random.

using UnityEngine;
using System.Collections;

public class ZoneMasterFunction : MonoBehaviour {

    public bool CreateTheNextZone = false;
    public GameObject[] ZonePool = new GameObject[4];
   
    // Update is called once per frame
    void SelectNextZone ()
    {
        if (CreateTheNextZone == true)
        {
            ZonePool[] = (Random.Range (0,4));
        }
    }
}

So I have loaded my 4 different world sections into the 4 different GameObject Slots. I’m Just not sure how I would get my script to select one of the objects at random. Ideally I then need the selected object to be removed from the list of possible available GameObjects. Untill it has moved off the far side of the screen and is then added to the available list again.

I have seen a few pages talking about Object Pools. But most of these seam to be on create bullets and prefabs of the same object. Is an object pool the right way to go with this? Or is there somewhere else I should be looking?

Thanks Guys.

Instantiate(ZonePool[Random.Range(0, ZonePool.Length)];
1 Like