Question by
ialam1 · Mar 15, 2018 at 11:17 PM ·
unity 5uiscenescore systemleaderboard
how to refresh the a scene without reloading it?
Hi,
In my scene i have a leader board. i have created a button which resets all the high scores to zero but you can only see the changes when you reload the scene. what do i do to make the values change instantaneously without reloading the scene.
// Use this for initialization
void Start ()
{
update = true;
for (int x=0; x< highScores.Length; x++)
{
highScoreValues[x] = PlayerPrefs.GetInt("highScoreValues" + x); //highScoreValues0 ,1,2,......
highScoreNames[x] = PlayerPrefs.GetString("highScoreNames" + x);
}
DrawScores();
}
public void resetHighScoresbutton()
{
for (int x = 0; x < highScores.Length; x++)
{
update = false;
PlayerPrefs.SetInt("highScoreValues" + x, 0); //sets highScoreValues0 ,1,2,......
PlayerPrefs.SetString("highScoreNames" + x, " "); //sets highScoreNames0 ,1,2,......
//SceneManager.LoadScene("scoreBoard");
}
}
void DrawScores()
{
for (int x = 0; x < highScores.Length; x++)
{
highScores[x].text = highScoreValues[x].ToString();
playernames[x].text = highScoreNames[x];
}
}
// Update is called once per frame
void Update () {
if (update==false)
{
DrawScores();
Debug.Log("function executed");
update = true;
}
}
}
Comment
Answer by Guy_Yome · Mar 16, 2018 at 10:25 PM
I think it is because you do not set the new highscore values in the variables used to display it. Read every portion of the code I sent that has a comment with ------------------------------------ at the end.
void Start ()
{
update = true;
// DO THE SAME THING BUT IN A REUSABLE FUNCTION INSTEAD ---------------------------------
update_score_variables();
DrawScores();
}
// SETS THE VALUES IN THE VARIABLES --------------------------------------------------
private void update_score_variables () {
for (int x=0; x< highScores.Length; x++)
{
highScoreValues[x] = PlayerPrefs.GetInt("highScoreValues" + x); //highScoreValues0 ,1,2,......
highScoreNames[x] = PlayerPrefs.GetString("highScoreNames" + x);
}
}
public void resetHighScoresbutton() {
for (int x = 0; x < highScores.Length; x++)
{
update = false;
PlayerPrefs.SetInt("highScoreValues" + x, 0); //sets highScoreValues0 ,1,2,......
PlayerPrefs.SetString("highScoreNames" + x, " "); //sets highScoreNames0 ,1,2,......
// SET THE NEW VALUES IN THE VARIABLE ---------------------------------------------
update_score_variables();
DrawScores();
//SceneManager.LoadScene("scoreBoard");
}
}
void DrawScores() {
for (int x = 0; x < highScores.Length; x++)
{
highScores[x].text = highScoreValues[x].ToString();
playernames[x].text = highScoreNames[x];
}
}
// Update is called once per frame
void Update () {
if (update == false) {
DrawScores();
Debug.Log("function executed");
update = true;
}
}
Your answer
Follow this Question
Related Questions
Can't score points 1 Answer
Scoring Points with UI Text 0 Answers
How to Remove a Button after it has been pressed and executed it's script? 3 Answers
OnGUI behind UI? 0 Answers