1
\$\begingroup\$

I don't know if this issue is in Unity or in my script, the score work well in scoreText, but in the scorePanel its show before last score like it showing 1 rather than 2; the issue doesn't always appear its appear just in specific moment when my score++ and player die nearly in same time, so here is my script. AddToScore is called with InvokeRepeating, scoreText who show me the correct score, it works well

void AddToScore(){
    score++;
    scoreText.text = score.ToString ();
    mySound.Play();
}

This is when my player Die

void OnCollisionEnter2D(Collision2D other)
{
    Time.timeScale = 0;
    Die ();
}

void Die (){
    Pause.SetActive (false);
    Panel.SetActive (true);
    GameOverPanel.SetActive (true);
    GenerateObstacles.HighScore ();
}

And here is the HighScore from GenerateObstacles

public void HighScore(){
    if (score > highScore) {
        PlayerPrefs.SetInt ("highScore", score);
        highScore = PlayerPrefs.GetInt ("highScore");
        scorePanel.text = score.ToString ();
        highScorePanel.text = highScore.ToString ();
    }
    else if (score <= highScore) {
        highScore = PlayerPrefs.GetInt ("highScore");
        scorePanel.text = score.ToString ();
        highScorePanel.text = highScore.ToString ();
    }
}

Here is a paint to understand

score issue

\$\endgroup\$
  • \$\begingroup\$ If somebody know how to resolve this issue, share it please :D \$\endgroup\$ – Markop Oct 17 '15 at 14:13
2
\$\begingroup\$

When player dies, Time.timescale is set to zero. So, you are stopping your application's clock. So time doesn't pass and AddToScore() will not be invoked.

\$\endgroup\$
  • 1
    \$\begingroup\$ You didn't understand me right, AddToScore() is already invoked because the scoreText (in AddToScore()) have changed, but scorePanel (in HighScore()) isn't changed and I don't know why \$\endgroup\$ – Markop Oct 14 '15 at 12:40
0
\$\begingroup\$

Looking at this: GenerateObstacles.HighScore (); It's not clear how the class is designed. HighScore() is not declared static. It would look like GenerateObstacle is a static reference, but it is not. One way is to declare a static reference in GeneratedObstacles like this : public static GeneratedObstacles current; then, in Awake(),

void Awake(){ current=this; }

With that, your code would change to : GeneratedObstacles.current.HighScore();

\$\endgroup\$
  • \$\begingroup\$ Thank you, but it didn't work, I always have the same problem, and what I don't understand why scorePanel doesn't change the value same to scoreText because they are using the same value \$\endgroup\$ – Markop Oct 15 '15 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.