Search Unity

Highscore counter not updating correctly

Discussion in 'Immediate Mode GUI (IMGUI)' started by DarkNeo, Sep 21, 2014.

  1. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    Hi there, Just wondering if anyone has had a similar problem to me.

    I have a object that travels in the x direction and it counts it's distance traveled and this is added up in the score counter, when you die it sends your score to the Highscore counter. everything works perfect and is counting up good.

    However this is the part that I'm quite stumped on. The Highscore is sometimes off by 1. Sometimes the score updates correctly into the Highscore when you reach a new one but most of the time it is always off by 1.

    I adjusted the Fixed Timestep to 0.0001 and that seems to make the Highscore update work slightly better but It's still not perfect.

    I have Googled around and I can't find anyone having the same problem as me. I can post my scripts if need be. I just wanted to see if anyone has heard of this before.


    Cheers!
     

    Attached Files:

  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,208
    That's a really bad idea since it makes physics run 10,000 times per second. Your score shouldn't be related to physics anyway, but in any case it's a problem somewhere in your scripts.

    --Eric
     
    DarkNeo likes this.
  3. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    Ok sorry It's taken a while but I still cannot figure out this Highscore script and why it randomly decides to display the highscore counter off by 1 like it my image at the top. it does it at random as well very very strange not sure what I am doing wrong hmm.

    Here are my scripts.

    My Camera has this script attached to it called "scoreCounter"

    Code (JavaScript):
    1. var myTransform : Transform;
    2. var origPos : Vector3;
    3. var currentPos : Vector3;
    4. var distance : int;
    5.  
    6.  
    7. function Start ()
    8. {
    9.     origPos=myTransform.position;
    10.     currentPos=myTransform.position;
    11. }
    12.  
    13. function Update()
    14.  
    15. {
    16.     distance += transform.position.x;
    17.        scorecount.scorecount = transform.position.x;
    18.    
    19. }
    My Object that is plugged into the script above has this script attached called "scorecount"

    Code (JavaScript):
    1. static var scorecount : int;
    2.  
    3. function Update ()
    4. {
    5.  
    6.     if (scorecount > PlayerPrefs.GetInt("score"))
    7.     {
    8.         PlayerPrefs.GetInt("score");
    9.         PlayerPrefs.SetInt("score",scorecount);
    10.        
    11.     }
    12.  
    13. }
    My GUI Text Score then has this script attached to it.

    Code (JavaScript):
    1. function OnGUI ()
    2. {
    3.     guiText.text = "Score : " + scorecount.scorecount;
    4.    
    5.    
    6. }
    My GUI text Highscore has this script attached to it.

    Code (JavaScript):
    1. function OnGUI ()
    2. {
    3.     guiText.text = "Highscore : " + PlayerPrefs.GetInt("score");
    4.    
    5.  
    6. }
    7.  
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,208
    Some things, which may or may not be related to the problem but should be fixed anyway:

    • PlayerPrefs shouldn't be done in Update or OnGUI. Use PlayerPrefs functions only once when appropriate, not multiple times every frame.
    • The OnGUI function is only for GUI and GUILayout functions, not the GUIText component (totally different thing).
    • "distance += transform.position.x" in Update is framerate-dependent and won't work; you need to account for framerate variance.
    • "PlayerPrefs.GetInt("score");" by itself does nothing; PlayerPrefs.GetInt returns a value and should be used as such.

    --Eric
     
    DarkNeo likes this.
unityunity