dimanche 12 janvier 2020

Unity 2D trouble with my randomiser script

This image is of the what the screen looks like at the start, I would like these to appear one by one. I have created this script to spawn my buttons randomly but do not know how to alter the times they individually spawn as they all spawn at the same time. They also show already when I load the scene which is not what I want as I can't see if they have spawned however in the Hierarchy they are being created .


public class RandomSpawn : MonoBehaviour {

    public GameObject prefab1, prefab2, prefab3, prefab4, prefab5;

    public float spawnRate = 2f;

    float nextSpawn = 0f;

    int whatToSpawn;

    // Update is called once per frame
    void Update() {

        if (Time.time > nextSpawn)
        {
            whatToSpawn = Random.Range(1, 6);

            switch (whatToSpawn)
            {
                case 1:
                    Instantiate(prefab1, transform.position, Quaternion.identity);
                    break;
                case 2:
                    Instantiate(prefab2, transform.position, Quaternion.identity);
                    break;
                case 3:
                    Instantiate(prefab3, transform.position, Quaternion.identity);
                    break;
                case 4:
                    Instantiate(prefab4, transform.position, Quaternion.identity);
                    break;
                case 5:
                    Instantiate(prefab5, transform.position, Quaternion.identity);
                    break;
            }

            nextSpawn = Time.time + spawnRate;
        }

What am I missing?




Aucun commentaire:

Enregistrer un commentaire