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

how can i increase the delay time of enemy spawn per sec

Discussion in '2D' started by charleszapanta, Sep 28, 2015.

  1. charleszapanta

    charleszapanta

    Joined:
    Sep 24, 2015
    Posts:
    7
    help me please. how can i make the enemy spawn faster every sec passes. upload_2015-9-28_22-58-33.png

    upload_2015-9-28_22-59-8.png
     
  2. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    556
    Use a Coroutine and WaitForSeconds(). Placing the code in Update() is not good practice. You don't need to check every single frame.
     
  3. charleszapanta

    charleszapanta

    Joined:
    Sep 24, 2015
    Posts:
    7
    Thanks for the reply! but, can you site me some sample. i dont get it. :(
     
  4. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    556
    @charleszapanta
    I have not tested this of course....but it will probably work.
    should place the following in Start();
    Code (CSharp):
    1. StartCoroutine(SpawnCar());
    and then, add this method:
    Code (CSharp):
    1. IEnumerator SpawnCar() {
    2.     spawning = true;
    3.     while (spawning) {
    4.         Vector3 carPos = new Vector3(Random.Range(-2.5f,2.5f),transform.position.y,transform.position.z);
    5.         Instantiate(car, carPos, transform.rotation);
    6.         yield return new WaitForSeconds(Delaytimer);
    7.     }
    8. }
    9.  
    You also need to add a boolean called spawning.
    To speed the calls up, you can change the delay timer in update, or another method.
     
    Last edited: Sep 29, 2015