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

I have a function called SpawnEnemies which I would like to loop three times then break to the next level

void SpawnEnemies(){
     foreach  (Transform child in transform){
           GameObject enemy = Instantiate (enemyPrefab, freePosition.position, Quaternion.identity) known as GameObject;
            enemy.transform.parent = freePosition;
    }
}

any help is greatly appreciated

Use code tags:

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.

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

hello !
You mean three times each time the screen is empty ?

yes three times the screen is empty

three waves before next level is triggered

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.