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

Coroutine count over score on game over

Discussion in 'Scripting' started by Rotzak, Aug 12, 2018.

  1. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    92
    Hello again,

    When the gameOverPage is loaded I want to count the score all over again. I want it so you clearly can see the score counting for +- 2 seconds. Currently the score is just immediately shown. I'm using a coroutine don't know if it's reall necessary.

    Here is my code:

    Code (CSharp):
    1. public void gameOverActivated()
    2.     {StartCoroutine(CountOverScore());
    3.     }
    4.  
    5. IEnumerator CountOverScore()
    6.     {
    7.         if (score > countOverCounter)
    8.         {
    9.             for (int i = 0; i < score; i++)
    10.             {
    11.               countOverCounter = i;
    12.               currentScoreOnGameOver.text = "Score: " + countOverCounter;
    13.             }
    14.         }
    15.         yield return null;
    16.     }
     
  2. Rioneer

    Rioneer

    Joined:
    Nov 27, 2013
    Posts:
    51
    Put yield return null; inside the nested for loop

    Code (CSharp):
    1. currentScoreOnGameOver.text = "Score: " + countOverCounter;
    2. yield return null;
     
  3. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    92
    already tried that gives me the same result
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    How large is the score value? The yield null will only wait til the next frame. If your score is, for example, ten, then the loop will complete very quickly (10 frames).
     
    Rioneer likes this.
  5. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    92
    The score is counting with one each second so a normal score to have is around 0 - 100.

    I've tested what u said by changing the value one to 10000. So each second the score is adding 10000 to the score. And you're right the code is working. Obviously I want to count one each second, so is there any way to fix this?
     
  6. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Try this:
    yield return new WaitForSeconds( 1 );
     
    Rioneer likes this.