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

Could anyone advise how to repeat a function 3 times then break

Discussion in 'Scripting' started by Mutation1, Apr 22, 2016.

  1. Mutation1

    Mutation1

    Joined:
    Apr 22, 2016
    Posts:
    6
    I have a function called SpawnEnemies which I would like to loop three times then break to the next level

    Code (CSharp):
    1. void SpawnEnemies(){
    2.      foreach  (Transform child in transform){
    3.            GameObject enemy = Instantiate (enemyPrefab, freePosition.position, Quaternion.identity) known as GameObject;
    4.             enemy.transform.parent = freePosition;
    5.     }
    6. }
    any help is greatly appreciated
     
    Last edited: Apr 22, 2016
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Use code tags:
    http://forum.unity3d.com/threads/using-code-tags-properly.143875/

    Call it 3 times?

    For loop from 1 to 3?

    Or, I'm assuming you want delays in time between them, because why would you spawn enemies and then immediately go to the next level.

    In which case, you'll want to look into Coroutines and WaitForSeconds.
     
  3. Mutation1

    Mutation1

    Joined:
    Apr 22, 2016
    Posts:
    6
    Thank you for your quick response.

    It is a space shooter game with the enemies spawning each time the screen is empty of enemies.

    I would like the function to respawn enemies three times and once the screen is cleared move to the next level.

    Newbie to programming by the way
     
  4. _HAL_9000

    _HAL_9000

    Joined:
    Jun 26, 2015
    Posts:
    48
    hello !
    You mean three times each time the screen is empty ?
     
  5. Mutation1

    Mutation1

    Joined:
    Apr 22, 2016
    Posts:
    6
    yes three times the screen is empty

    three waves before next level is triggered
     
  6. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    897
    add the enemies to a list/array stored in the spawner script (or perhaps better handled as a static list on the enemy script, so that other scripts could use it). when you spawn them, have the spawner listen to a static event "OnDeath" that is raised from the enemy when they are killed (via damage, not the destroy command, to avoid bugs where a level is restarted or returned to the main menu which could cause you going to the next level). the spawner will be listening to this event, removing the enemy from their list

    after which the spawner can check if the list is empty. If so, spawn the next wave, otherwise load the next level.