[Solved] Spawn objects randomly ?

I want to spawn amount of objects when I press a button. what happing with me is giving only one object. I need random of objects. example when i press X it gives 1,2, 0 or 3 objects.

    public GameObject[] diamons; // 3 objects

    void Update ()
    {
        Vector3 place = transform.position;

        if (Input.GetKeyDown(KeyCode.X))
        {
            int diamons_Num = Random.Range(0,3);
            Instantiate(diamons[diamons_Num], place, Quaternion.identity); 
            Debug.Log (" .:. Surprice .:.");
        }
          
        } // end update

You need to have a for loop. So something like
Place this after your random.range call.

for(int x = 0; x <= diamons_Num;x++)
{
    //Instantiate your object here
}

Also note because you are using random.range on ints, the 3 is not included. So if you want 0,1,2,3 you need to do random.range(0,4). If you want a random object along with a random number of said object, you will need another random number.

1 Like

Invoke

1 Like

Thank you so much.
[Solved]