Search Unity

Question Spawn Enemies at array of spawnpoints

Discussion in 'Scripting' started by getmyisland_dev, Jul 24, 2021.

  1. getmyisland_dev

    getmyisland_dev

    Joined:
    Dec 22, 2020
    Posts:
    100
    I want to spawn enemies at a random spawnpoint from an array.

    I have a spawnpoint array and a basic spawn scripts. How can I now pick a random spawnpoint from this array and let the game decide where to spawn.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemySpawner : MonoBehaviour
    6. {
    7.     public GameObject normalEnemyPrefab;
    8.  
    9.     public GameObject[] SpawnPoints;
    10.  
    11.     Vector2 whereToSpawn;
    12.  
    13.     public float spawnRate = 4;
    14.  
    15.     float nextSpawn = 0.0f;
    16.  
    17.     void Start()
    18.     {
    19.        
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.         if(Time.time > nextSpawn)
    25.         {
    26.             nextSpawn = Time.time + spawnRate;
    27.  
    28.            
    29.  
    30.             Instantiate(normalEnemyPrefab, whereToSpawn, Quaternion.identity);
    31.         }
    32.     }
    33. }
    34.  
    There should also be a small cooldown on the spawnpoints so that there are never 2 enemies at one spawnpoint.

    How can I now start to code this? Thanks for your help.
     
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    Code (csharp):
    1.  
    2. Vector3 whereToSpawn = SpawnPoints[Random.Range(0,SpawnPoints.Length)].transform.position;
    3.  
    Make sure the array is filled with game objects or you'll recive a null reference exception. Better to test for thiss additionally.
     
    getmyisland_dev likes this.
  3. getmyisland_dev

    getmyisland_dev

    Joined:
    Dec 22, 2020
    Posts:
    100
    Thank you this works perfectly. Is there a way to store the whereToSpawn spawnPoint and tell the game that it can't select this spawnpoint in the next spawn wave.

    And again thank you very much.
     
  4. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    Easiest solution would be to use a List instead of an Array. Then remove the point where you spawn an enemy. If you need to spawn them again after all points are used, just store them in a new List for reuse.
    If you just use some kind of "flag" to remember which point has already been used, randomness can "kick" you and you need to iterate multiple times until you find an unused spot. The List where used points are removed always provide a valid point which is unused. When it is empty you just switch to the new one which contains the already used ones.
    Or you could just "shuffle" the list/arrays contents (look up fisher-yates shuffle) and then just use the next point in the randomly ordered list. When you reach the end shuffle again and start from zero index.