vendredi 25 octobre 2019

how to Pick random GameObject in Unity

I'm making an endless runner game, and I have 4 types of terrain I want to auto-generate. the auto-generation works, but I want to select a random terrain but I can't figure out how. I have tried this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlatformManager: MonoBehaviour
{
    [SerializeField]
    private GameObject[] _platformPrefabs;
    [SerializeField]
    private int _zedOffset;


    // Start is called before the first frame update
    void Start()
    {
        randomTerrain();
    }

    public void RecyclePlatform(GameObject Platform)
    {
        Platform.transform.position = new Vector3(0, 0, _zedOffset);
        _zedOffset += 12;

    }

    public void randomTerrain()
    {
        for (int i = 0; i < _platformPrefabs.Length; i++)
        {
            Instantiate(_platformPrefabs[Random.Range(0, 3)], new Vector3(0, 0, i * 12), Quaternion.Euler(0, 90, 0));
            _zedOffset += 12;
        }
    }

}

but it only selects the first game object every time except the first initiation. how can I make it select randomly?

Inspector view of script




Aucun commentaire:

Enregistrer un commentaire