Why not use Random.Range here?

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

public class AttackerSpawner : MonoBehaviour
{
   
    bool spawn = true;
    [SerializeField] Attacker attackerPrefab;
    [SerializeField] float waitTimeMinimum;
    [SerializeField] float waitTimeMaximum;
    // Start is called before the first frame update
     IEnumerator Start()
    {
        while(spawn)
        {
           
            yield return new WaitForSeconds(Random.Range(waitTimeMinimum, waitTimeMaximum));
            SpawnAttacker();
        }
    }

    void SpawnAttacker()
    {
        Instantiate(attackerPrefab, new Vector3(8, Random.Range(1,6), 0), Quaternion.identity);

    }
  // IEnumerator spawnOcto()
  //  {
        
   // }

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

It works just fine and spawns the enemies in random positions exactly like the video but without having to make seprate instances of the same object

Hi Xarce,

Using Random.Range is a good idea. :slight_smile:

Almost all “why not” questions can be answered with: “Because [instructor] cannot show everything and because there are often multiple ways to do something in Unity”.


See also:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms