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

Loop with delay

Discussion in 'Scripting' started by S3M1CZ, Nov 23, 2019.

  1. S3M1CZ

    S3M1CZ

    Joined:
    Feb 11, 2017
    Posts:
    14
    I'm fairly new to Unity and I was trying to create a primitive city simulation with a population counter that would update its text component every two seconds.

    I've tried Coroutines and InvokeRepeating but none of them seemed to work as I wanted to (there was a 2 second delay at first but after that the text component updated every frame).

    Attempt I.
    Code (CSharp):
    1. void Update()
    2.     {
    3.         StartCoroutine(DisplayPopulation());
    4.     }
    5.  
    6.     IEnumerator DisplayPopulation()
    7.     {
    8.         populationText.text = population.ToString();
    9.         population += 100;
    10.         yield return new WaitForSeconds(2);
    11.     }
    Attempt II.
    Code (CSharp):
    1. void Update()
    2.     {
    3.         StartCoroutine(DisplayPopulation());
    4.     }
    5.  
    6.     IEnumerator DisplayPopulation()
    7.     {
    8.         while (true)
    9.         {
    10.             populationText.text = population.ToString();
    11.             population += 100;
    12.             yield return new WaitForSeconds(2);
    13.         }
    14.     }
    Attempt III.
    Code (CSharp):
    1. void Update()
    2.     {
    3.         InvokeRepeating("PopulationCount", 2f, 2f);
    4.     }
    5.  
    6.     void PopulationCount()
    7.     {
    8.         populationText.text = population.ToString();
    9.         population += 100;
    10.     }
    I've probably just understood these poorly and I apologise for that but I've been unable to find a proper solution for this (or at least one that I would be able to understand).

    Thanks in advance for any help or advice!
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Attempt 1 would just wait a couple of seconds before ending the coroutine, however, you are starting a new routine every frame.

    Attempt 2 would work if you started the routine just once, say in the Start(). You are currently starting a new routine every frame in Update() just as with 1.

    Attempt 3 has the same problem again. You are setting an instruction to invoke the method every frame. Do it just once in Awake() or Start() and it will work.
     
    Last edited: Nov 23, 2019
    S3M1CZ likes this.
  3. S3M1CZ

    S3M1CZ

    Joined:
    Feb 11, 2017
    Posts:
    14
    Moved the Coroutine to the Start() and it works as it should, thanks! :)
     
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Start() can be a coroutine, so you could just do this is you like;
    Code (CSharp):
    1.  
    2. private WaitForSeconds _waitTwoSecs = new WaitForSeconds(2);
    3.  
    4. private IEnumerator Start()
    5. {
    6.     while (true)
    7.     {
    8.         populationText.text = population.ToString();
    9.         population += 100;
    10.         yield return _waitTwoSecs;
    11.     }
    12. }
     
    S3M1CZ likes this.