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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help with spawning a certain amount of enemies

Discussion in 'Scripting' started by Chris75, Jan 5, 2018.

  1. Chris75

    Chris75

    Joined:
    Jan 31, 2015
    Posts:
    41
    Hi all. I am pooling my enemies and everything works fine. I am however changing my gameplay to where a level is complete after you kill 80 enemies, and not have it be endless.

    I took a year or so break from this hobby and am confused on how to do this. Any help would be appreciated. Here is a sample of my current code:

    Code (CSharp):
    1.     public GameObject[] enemies;
    2.  
    3.     IEnumerator SpawnEnemies(int index, float seconds)
    4.     {
    5.         yield return new WaitForSeconds(seconds);
    6.         //Instantiate(enemies[index], enemies[index].transform.position, enemies[index].transform.rotation);
    7.         SimplePool.Spawn(enemies[index], enemies[index].transform.position, enemies[index].transform.rotation);
    8.     }
    9.  
    10.     void Update()
    11.     {
    12.         score = ScoringStoryCity.scoreCityStory;
    13.  
    14.         if (score <= 20)
    15.         {                                    
    16.             int enemyIndex = Random.Range(0, enemies.Length);
    17.             StartCoroutine(SpawnEnemies(enemyIndex, (0.50f, 0.50f));
    18.         }
    I currently just have the enemies destroyed once score equals 80. But I don't like how that looks in game. It's too unfinished and lazy IMO. I would rather just keep my pooling, limited to at most 80 enemies.

    Any help would be appreciated.
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
  3. Chris75

    Chris75

    Joined:
    Jan 31, 2015
    Posts:
    41
    I have a separate script called SimplePool. Believe I got it from here actually. Got it a few years ago. I will review the video to see if it's all up to date. Thanks for the concern.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    One of your first issues here is that you're starting a new coroutine when the score is <= 20, inside Update(). This could happen many, many times. Since that value has the potential ( I imagine) to be the same for a long time , and satisfy that condition.. that would be an issue.
    You should decide when you can tell that the condition to spawn enemies is met, and then do it at that time.

    Another issue is that it looks as though the parameters you're passing in don't match the signature (/ are ill-formed).

    Then, if what you want is a certain number of enemies in the wave, write the coroutine in a manner that repeats at an interval to spawn that many (or all at once, if that's what you want).
    Do you just need help with a counter or loop or something like that?
     
  5. Chris75

    Chris75

    Joined:
    Jan 31, 2015
    Posts:
    41
    Sorry. I know my code is sloppy for now. I am going to clean lots of it up once I am happy with the project.

    As of now, I have slower enemies spawning from 0-20 score, then they get harder and spawn in less time than the .50f.

    I will do some research in the counter and/or loops. I do not have any in my project currently. I can never turn down help though! Even if it's a finger in the right direction. Thanks
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hm..Okay, well did you understand the part I wrote about the update method calling the coroutine and why that's not good?
    How many enemies are you trying to spawn, or is it continuous at a certain rate/time interval?
     
  7. Chris75

    Chris75

    Joined:
    Jan 31, 2015
    Posts:
    41
    Honestly, I am unsure why it is bad practice to call on a coroutine in an update.

    My current setup, it's a continuous/endless spawn. Once the score hits 80, I disable the enemies. But 81-82 will end up spawning because by the time the score of 80 hits, 1 or 2 more have spawned.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, continuous/endless spawning until 80 is your goal.
    Do you adjust the score after the spawn? Because I see it has a wait delay at the start.

    But at every frame , you're starting so many routines? That was my question/point.

    Why not simply have the wait, check score (for making them harder), spawn .. inside the coroutine? break out of the coroutine when you've hit 80.