- Home /
Spawn various enemies at random
Hi All, I am wanting to write an enemy spawning script that chooses randomly from several types of enemies. Would it be most efficient to use 1 prefab, and just change the sprite at random when it spawns? I have the following script which works for a single prefab, I think I need to create a list instead of just the single gameobject "enemy", but dont know where to start! Thanks.
Here is my script so far:
using System.Collections; using System.Collections.Generic; using UnityEngine;
 public class EnemySpawnerScript : MonoBehaviour    {
     public GameObject enemy;
     float randx, randy;
     Vector2 whereToSpawn;
     public float spawnRate = 2f;
     float nextSpawn = 0.0f;
 
     // Start is called before the first frame update
     void Start()    
 {
          }
     // Update is called once per frame
     void Update()    
 {
       
 if (Time.time > nextSpawn)
     {
         nextSpawn = Time.time + spawnRate;
         randx = Random.Range(10f, 10f);
         randy = Random.Range(-4.5f, 4.5f);
         whereToSpawn = new Vector2(randx, randy);
         Instantiate(enemy, whereToSpawn, Quaternion.identity);
     }
  
     }
    
 }
Answer by Calamar1e · Mar 12, 2021 at 07:20 PM
Try this @davepottsyork :
      //define the amount of enemy types you can spawn in the hierarchy
      public GameObject[] Enemies;
      float randx, randy;
      Vector2 whereToSpawn;
      public float spawnRate = 2f;
      float nextSpawn = 0.0f;
  
      // Start is called before the first frame update
      void Start()    
  {
           }
      // Update is called once per frame
      void Update()    
  {
        
  if (Time.time > nextSpawn)
      {
          nextSpawn = Time.time + spawnRate;
          randx = Random.Range(10f, 10f);
          randy = Random.Range(-4.5f, 4.5f);
          whereToSpawn = new Vector2(randx, randy);
          Instantiate(enemy, whereToSpawn, Quaternion.identity);
      }
   
      }
Thanks. Would I just list the various enemies in the [] eg asteroid1;asteroid2;asteroid3; ?
it should appear in the hierarchy, you can define the number of enemy types in by dragging in the the prefabs to various slots
 This is my code:
 [SerializeField]
     private float spawnRadius = 7f, time = 3f;
 
     public GameObject[] Enemies;
 
     public GameObject SpawnFx;
 
     // Start is called before the first frame update
     void Start()
     {
         StartCoroutine(SpawnAnEnemy());
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 
     IEnumerator SpawnAnEnemy()
     {
         GameObject Impact = Instantiate(SpawnFx, transform.position, Quaternion.identity);
 
         Vector2 spawnPos = GameObject.Find("Player").transform.position;
 
         spawnPos = Random.insideUnitCircle.normalized * spawnRadius;
 
         Instantiate(Enemies[Random.Range(0, Enemies.Length)], spawnPos, Quaternion.identity);
 
         Destroy(Impact, 0.3f);
 
         yield return new WaitForSeconds(time);
 
         StartCoroutine(SpawnAnEnemy());
     }
Your answer
 
 
             Follow this Question
Related Questions
Random element from the list 1 Answer
A node in a childnode? 1 Answer
Random select from array and spawn 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                