Search Unity

Spawning Enemies in a Repeating Pattern

Discussion in 'Scripting' started by Carter0, May 10, 2016.

  1. Carter0

    Carter0

    Joined:
    Feb 12, 2016
    Posts:
    62
    Hey everyone, I am trying to make a space shooter game where enemies spawn repeatedly. I tried to make them spawn after a certain amount of time, but I could not figure it out. I made code below that should spawn 10 enemies repeatedly as well, but the code only spawns one. Can someone help me out?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Spawn : MonoBehaviour
    5. {
    6.     public GameObject [] spawnpoints;
    7.     public GameObject Enemy;
    8.     private int i;
    9.  
    10.  
    11.  
    12.     void Start ()
    13.     {
    14.        
    15.         spawnpoints = GameObject.FindGameObjectsWithTag ("Spawnpoint");
    16.         i = 1;
    17.  
    18.  
    19.         while (i < 10)
    20.         {
    21.             GameObject spawn = spawnpoints [Random.Range (0, spawnpoints.Length)];
    22.             Instantiate (Enemy, spawn.transform.position, spawn.transform.rotation);
    23.             i = i + 1;
    24.         }
    25.  
    26.            
    27.     }
    28.        
    29. }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. void Start()
    3.  
    called once at the start of the life of the gameobject

    Code (csharp):
    1.  
    2. while(i <10)
    3.  
    entire loop code executed in a single frame, there is no concept of time here...

    by the looks of it this code should be producing "one wave" of 10... was that what you mean?
     
    Carter0 likes this.
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148


    might be of interest
     
    Carter0 likes this.
  4. Carter0

    Carter0

    Joined:
    Feb 12, 2016
    Posts:
    62
    I realized that the loop had no time parameters when I asked the question, and that hurdle was something I tried to overcome when I first started trying to make spawn waves. But I could not figure it out so i decided to break down the problem further by just getting enemies to spawn one after another.

    I want the code to make 10 enemies back to back basically. Thats what I mean.

    Ill go check that video now.
     
  5. Carter0

    Carter0

    Joined:
    Feb 12, 2016
    Posts:
    62
    Yeah that video was informative and helpful. Ill go update my code and if I still need help ill post tomorrow I guess. But hopefully this video should be able to help me out a lot.
     
  6. UWantMyName

    UWantMyName

    Joined:
    Jul 9, 2018
    Posts:
    2

    If you want to spawn enemies at a certain amount of time, you have to use Coroutines.
    For example, let's say that you want to spawn enemies once every 0.5 seconds.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Spawn : MonoBehaviour
    6. {
    7.     public GameObject [] spawnpoints;
    8.     public GameObject Enemy;
    9.     private int i = 0;
    10.  
    11.     private IEnumerator SpawnEnemies()
    12.     {
    13.         GameObject spawn = spawnpoints[Random.Range(0, spawnpoints.Length)];
    14.         Instantiate(Enemy, spawn.transform.position, spawn.transform.rotation); // I would honestly use Quaternion.identity instead of spawn.transform.rotation, but it is up to you
    15.         i++;
    16.         yield return new WaitForSeconds(0.5f); // or you can put any amount of seconds in here
    17.         if (i < 10) StartCoroutine(SpawnEnemies());
    18.         // don't need an else because it won't do anything when the condition is not met
    19.     }
    20.  
    21.     void Start ()
    22.     {
    23.      
    24.         spawnpoints = GameObject.FindGameObjectsWithTag ("Spawnpoint");
    25.         StartCoroutine(SpawnEnemies());  
    26.     }
    27.      
    28. }
    29.  

    Please do let me know if it works. :)
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    Your advice might be a tad bit late, since the post you're replying to is four years old.

    But just a tad. They've probably got the project on hold waiting for exactly this thread to get an answer.