Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Using a Coroutine to spawn enemies with different spawning locations?

Discussion in 'Scripting' started by Chris75, Feb 16, 2015.

  1. Chris75

    Chris75

    Joined:
    Jan 31, 2015
    Posts:
    41
    So I am moving from InvokeRepeating to spawn enemies to a Coroutine.

    Coroutines still confuse me, but it works better in my case than InvokeRepeating.

    So, I am using a Coroutine for an array of enemies to spawn towards the player. However, they all spawn in the same spot. In my InvokeRepeating I never had this issue because I placed the gameobject at the spawn point I want before making it a prefab. So in the prefab the coordinates are correct.

    But for the coroutine, they all spawn where the Coroutine script is placed on my game (whether I put it in my empty game object for scripts, or my background)

    Here is my code:

    Code (csharp):
    1.  
    2. public class EnemiesCoroutine : MonoBehaviour {
    3.  
    4.  
    5.         int score2 = Scoring.score;
    6.     bool isSpawning = false;
    7.     public GameObject[] enemies;
    8.    
    9.    
    10.     IEnumerator SpawnObject(int index, float seconds)
    11.     {
    12.        
    13.         transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
    14.        
    15.         Debug.Log ("Waiting for " + seconds + " seconds");
    16.        
    17.         yield return new WaitForSeconds(seconds);
    18.  
    19.         Instantiate(enemies[index], transform.position, transform.rotation);
    20.        
    21.        
    22.         isSpawning = false;
    23.     }
    24.    
    25.     void Update ()
    26.     {
    27.  
    28.         score2 = Scoring.score;
    29.        
    30.         if(! isSpawning && score2 >= 0)
    31.         {
    32.             isSpawning = true;
    33.             int enemyIndex = Random.Range(0, enemies.Length);
    34.             StartCoroutine(SpawnObject(enemyIndex, Random.Range(3, 3)));
    35.         }
    36.        
    37.         else if ( !isSpawning && score2 <= 5)
    38.         {
    39.             isSpawning = true;
    40.             int enemyIndex = Random.Range(0, enemies.Length);
    41.             StartCoroutine(SpawnObject(enemyIndex, Random.Range(1, 1)));
    42.         }
    43.     }
    44.    
    45.  
    I've tried to search for help on this but Google came up empty.

    Any help would be appreciated.
     
  2. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    In SpawnObject, this line looks redundant because it is just assigning itself the value it already has.

    I guess you need to spawn the object at enemies[index] transform and not the EnemiesCoroutine transform, so
    it could be something like this instead :

    Instantiate(enemies[index], enemies[index].gameobject.transform.position, enemies[index].gameobject.transform.rotation);
     
    Chris75 likes this.
  3. Chris75

    Chris75

    Joined:
    Jan 31, 2015
    Posts:
    41
    Thanks for the reply, it's much appreciated.

    The original code you listed did not work, but when I changed it to:

    Code (CSharp):
    1. Instantiate(enemies[index], enemies[index].transform.position, enemies[index].transform.rotation);
    It worked perfectly. So it's still your doing, so thank you.

    I also removed the line you said to. I didn't realize that. So thanks again, it is much appreciated!

    And thanks for sharing that link in your signature, I plan to implement that down the road.
     
  4. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    Glad it worked :)