A
A
accacha2021-04-29 17:03:20
Unity
accacha, 2021-04-29 17:03:20

How to spawn only one random array element?

There is an empty object, on which the script for spawning objects from the array is superimposed. How to make it so that only one random element of the array spawns? It turned out such a script, but all the elements of the array will still spawn.

using UnityEngine;

public class SpawnObject : MonoBehaviour
{

    public GameObject[] objects;
    
    void Start()
    {
        int rand = Random.Range(0, objects.Length);
        GameObject instance = Instantiate(objects[rand], transform.position, Quaternion.identity);
        instance.transform.parent = transform;
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question