How to randomize my two GameObjects, so only 1 spawns at a time.

Hello, I’m learning to code and making a flappy bird inspired game. I have 2 different types of pipes, exactly the same just different colors. I want the game to spawn 1 of the 2 pipes at the time randomly. Any help or advice would be greatly appreciated.

public class PipeSpawner : MonoBehaviour
{
    public float maxTime = 1.5f;
    float timer = 0;
    public GameObject Column;
    public GameObject Column2;
    public float minHeight;
    public float maxHeight;

    // Start is called before the first frame update
    void Start()
    {
        GameObject newColumn = Instantiate(Column);
        newColumn.transform.position = (Vector2)transform.position + Vector2.up * Random.Range(minHeight, maxHeight);
        Destroy(newColumn, 5);
    }

    // Update is called once per frame
    void Update()
    {
        if(timer > maxTime)
        {
            GameObject newColumn = Instantiate(Column);
            newColumn.transform.position = (Vector2)transform.position + Vector2.up * Random.Range(minHeight, maxHeight);
            Destroy(newColumn, 5);
            timer = 0;
        }

        timer += Time.deltaTime;
    }
}

When you choose what to instantiate, pull them out of an array:

public GameObject[] PossiblePipes;

and use Random.Range() to choose.

int whichPipe = Random.Range( 0, PossiblePipes.Length);

and off you go!

2 Likes

Thank you for your reply, I’m confused about the second part. What would I need to delete and replace that line with?

The array is the best option, but if you only have two pipe kinds, try this:

int rand = Random.Range(0, 2); // returns 0 or 1 (not 2!)
if (rand == 0) {
// instantiate Column
} else {
// instantiate Column2
}
2 Likes

Thank you for your reply, I’m still a bit confused.

public class PipeSpawner : MonoBehaviour
{
    public float maxTime = 1.5f;
    float timer = 0;
    public GameObject Column;
    public GameObject Column2;
    public float minHeight;
    public float maxHeight;

    // Start is called before the first frame update
    void Start()
    {
        int rand = Random.Range(0, 2);
        if (rand == 0)
        {
            Instantiate(Column);
        }
        else
        {
            Instantiate (Column2);
        }
        GameObject newColumn = Instantiate(Column);
        newColumn.transform.position = (Vector2)transform.position + Vector2.up * Random.Range(minHeight, maxHeight);
        Destroy(newColumn, 5);
    }

    // Update is called once per frame
    void Update()
    {
        if(timer > maxTime)
        {
            GameObject newColumn = Instantiate(Column);
            newColumn.transform.position = (Vector2)transform.position + Vector2.up * Random.Range(minHeight, maxHeight);
            Destroy(newColumn, 5);
            timer = 0;
        }

        timer += Time.deltaTime;
    }
}

Try a search “Unity random from array” Lots of good answers.

Oh, I abbreviated the code. Here’s how it should look:

void Start()
    {
GameObject newColumn; // declared outside the if block so it has proper scope
        int rand = Random.Range(0, 2);
        if (rand == 0)
        {
            newColumn = Instantiate(Column);
        }
        else
        {
            newColumn = Instantiate(Column2);
        }

        newColumn.transform.position = (Vector2)transform.position + Vector2.up * Random.Range(minHeight, maxHeight);
        Destroy(newColumn, 5);
    }