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

Make a Dynamic Final Score

Discussion in 'Scripting' started by SteelDragonBR, May 6, 2015.

  1. SteelDragonBR

    SteelDragonBR

    Joined:
    Mar 24, 2015
    Posts:
    18
    Hello,

    I try this
    float b = GetComponent<Gameplay> ().GetScore ();
    for (float i=0f; i<=b; i++) {
    maxScore.text = "Score:" + i.ToString ("0");
    }

    But the variable i dont work as expected. I want start in 0 and stop the counter in final score number.
     
  2. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    What does not work as expected? What is expected, and what does it do?
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,999
    SteelDragonBR likes this.
  4. Brainswitch

    Brainswitch

    Joined:
    Apr 24, 2013
    Posts:
    270
    What do you want to do? My guess is that you want the maxScore text to show 0, then increment and after X time show the GetScore number. Or? Where are you using this code? In an Update() function? Unless you yield after the
    Code (CSharp):
    1. maxScore.text = "Score:" + i.ToString ("0");
    line, your for loop is essential the same thing as writing
    Code (CSharp):
    1. maxScore.text = "Score:" + b.ToString ("0"); // where b is equal to GetComponent<Gameplay> ().GetScore ()
    .
    Ah, mgear wrote during me writing this post. But yes, you could look into Coroutines and/or use Invoke/InvokeRepeating.
     
    SteelDragonBR likes this.
  5. SteelDragonBR

    SteelDragonBR

    Joined:
    Mar 24, 2015
    Posts:
    18
  6. SteelDragonBR

    SteelDragonBR

    Joined:
    Mar 24, 2015
    Posts:
    18
    Thank You! This code is inside of the method that is invoked when the game is finished. Without void Update(). I just want see the numbers change slowly in the level finished screen.
     
  7. SteelDragonBR

    SteelDragonBR

    Joined:
    Mar 24, 2015
    Posts:
    18
    Solved

    Code (CSharp):
    1. private IEnumerator ShowScore()      
    2.     {
    3.  
    4.  
    5.         float b = GetComponent<Gameplay> ().GetScore ();
    6.         for (float i=0; i<=b; i++)
    7.         {
    8.             yield return new WaitForSeconds(-0.1f);
    9.             maxScore.text = "Score:" + i.ToString ("0") ;
    10.  
    11.         }
    12.    
    13.     }
    Thank you!
     
  8. Brainswitch

    Brainswitch

    Joined:
    Apr 24, 2013
    Posts:
    270
    Code (CSharp):
    1. yield return new WaitForSeconds(-0.1f);
    You probably want 0.1f (non-negative), although I guess 0.1f is a bit too much for the effect you want, since negative WaitForSeconds is the same as zero (and then it is better to just use yield return 0)
     
    SteelDragonBR likes this.
  9. SteelDragonBR

    SteelDragonBR

    Joined:
    Mar 24, 2015
    Posts:
    18
    Yep, thanks! 0.001f works great too.
     
  10. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,999
    also this would wait for 1 frame:
    Code (CSharp):
    1. yield return null;
     
    SteelDragonBR likes this.
  11. SteelDragonBR

    SteelDragonBR

    Joined:
    Mar 24, 2015
    Posts:
    18
    Thank you! Nice information!