Search Unity

Spawning 10 little enemies when normal enemie die

Discussion in 'Scripting' started by dlazaroaquillue, Apr 28, 2017.

  1. dlazaroaquillue

    dlazaroaquillue

    Joined:
    Apr 19, 2017
    Posts:
    7
    Hi everyone, im trying to create 10 little enemies in the position of a normal enemy when the normal enemy dies. But , i dont know how can i do it . I have this:

    Code (CSharp):
    1. public class WaveSpawnerDie : MonoBehaviour
    2. {
    3.     //Prefab i want to load
    4.     public GameObject enemy;
    5.     //Position i want to spawn enemies
    6.     public Transform spawnPoint;
    7.  
    8.     void Update()
    9.     {
    10.         //If my normalenemy prefab dies then i want to spawn my 10 little enemies
    11.         if (EnemyKill.isDead)
    12.         {
    13.             StartCoroutine(SpawnWave());
    14.         }
    15.     }
    16.     //Here to spawn little enemies waiting between em 1 sec
    17.     IEnumerator SpawnWave()
    18.     {
    19.         for (int i = 0; i < 10; i++)
    20.         {
    21.             SpawnEnemy(enemy);
    22.             yield return new WaitForSeconds(1f);
    23.         }
    24.     }
    25.  
    26.     void SpawnEnemy(GameObject enemy)
    27.     {
    28.         Instantiate(enemy, spawnPoint.position, spawnPoint.rotation);
    29.     }
    30. }
    Hope u can help me . Thx
     
    Last edited: Apr 28, 2017
  2. laxbrookes

    laxbrookes

    Joined:
    Jan 9, 2015
    Posts:
    235
    You are spawning them all in the exact same place. Do they appear in your hierarchy ?
     
  3. dlazaroaquillue

    dlazaroaquillue

    Joined:
    Apr 19, 2017
    Posts:
    7
    No , they didnt appear when my enemy dies.
     
  4. laxbrookes

    laxbrookes

    Joined:
    Jan 9, 2015
    Posts:
    235
    Where are you changing your isDead value?
     
  5. dlazaroaquillue

    dlazaroaquillue

    Joined:
    Apr 19, 2017
    Posts:
    7
    Im changing in my Enemy script. isdead is a public static bool.
    Code (CSharp):
    1.     public void Die()
    2.     {
    3.         isDead = true;
    4.         GameObject effect = (GameObject)Instantiate(deathEffect, transform.position, Quaternion.identity);
    5.         Destroy(effect, 3f);
    6.         Destroy(gameObject);
    7.     }
     
  6. laxbrookes

    laxbrookes

    Joined:
    Jan 9, 2015
    Posts:
    235
    If it is a public STATIC bool, it will not be reassigned. Remove the static identifier from it.

    Don't even listen to me
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Not sure why it's not showing up, but I am pretty sure that you want to call this CoRoutine once when the enemy dies.
    Ergo, re-organize your code so it's not being checked in Update.
    You will essentially fire off dozens of these coroutines per second, until that variable is changed... you only want 1.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Pardon? You can assign (or re-assign) a static like that.
     
  9. laxbrookes

    laxbrookes

    Joined:
    Jan 9, 2015
    Posts:
    235
    For some reason, I was convinced I'd read const. Even though I'd written that too... Apologies.
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    heh, no prob.. Just clearing that up for him (and maybe you.. but not you, as it seems) :)
     
    laxbrookes likes this.
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Not sure where your spawner script is or where it gets its information for position..
    Do you have something like, the enemy dies -> spawns an empty game object and on that game object is a script to spawn the little enemies?
    That's just 1 idea, I guess..
    I would just turn Start into an IEnumerator (or call your CoRoutine from Start) on a script like.. then you know the position easily. no need for Update anymore. and once all ten are spawned, it can be destroyed.

    Still not sure why nothing spawned, but you could give this a shot, if you're still searching :)
     
  12. laxbrookes

    laxbrookes

    Joined:
    Jan 9, 2015
    Posts:
    235
    Is your gameobject being destroyed too soon?
    What is your WaveSpawnerDie attached to?
     
  13. dlazaroaquillue

    dlazaroaquillue

    Joined:
    Apr 19, 2017
    Posts:
    7
    Thx for all the help, let me try this and ill notice u ! :)
     
  14. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ok :) Good luck.
     
  15. dlazaroaquillue

    dlazaroaquillue

    Joined:
    Apr 19, 2017
    Posts:
    7
    Hello! i tried to use the empty gameObject to spawn enemies and didnt work after 2 hours of trying it . Maybe i can change to another mob i dont know.. w/e ill try to finish this mob. Thx for help!
     
  16. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Post your code? Dunno sorry man.. the idea sounds sensible in my head..
    Try whatever you want, if you post the code you tried before I'll look at it when I can. might be off to sleep soon.