- Home /
Displaying the score after a reset.
Hello. I am working on a game that is for the Android and is 2D. I am having a problem with displaying score. The script I use involves a Void Awake that is set to reset the score ( Which is what I want. ) When the game is finished it takes you to a different scene that is the menu. I want the latest score to be displayed on that menu instead of just resetting it to zero. I am using this script for the Score
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreManager : MonoBehaviour
{
public static int score; // The player's score.
Text text; // Reference to the Text component.
void Awake ()
{
// Set up the reference.
text = GetComponent <Text> ();
score = 0;
}
void Update ()
{
// Set the displayed text to be the word "Score" followed by the score value.
text.text = "Score: " + score;
}
}
The score is updated by each click on a gameObject in the game, if useful I will put that script on to. I have made a different post where I just removed the Awake function, but resetting the score seems impossible. The name of the UIText where the score is displayed is called ScoreText. All help is very appreciated.
you need to put that script or atlesat the code snippet where and how you update score.
This is the Script that updates the score.
using UnityEngine;
using System.Collections;
public class Score : $$anonymous$$onoBehaviour {
public int scoreValue = 1;
// Update is called once per frame
void On$$anonymous$$ouseDown () {
Score$$anonymous$$anager.score += scoreValue;
}
}
Remove score = 0; From Awake and add this below Update.
void OnLevelWasLoaded(int level) { Score$$anonymous$$anager.score = 0; }
Code is from this LInk: https://stackoverflow.com/questions/26934920/how-do-i-restart-my-score-reset-static-score-in-reloading-scene-in-unity
Answer by hbalint1 · Apr 03, 2015 at 08:17 PM
Hello. I asked a question recently. I think it will work for you as well.
http://answers.unity3d.com/questions/939809/find-uninstantiated-object-and-change-property.html
In the menu make the ScoreText to a public static string ScoreText;
property and you can change it from other scene. So when the game ends you can change the text with ScriptName.ScoreText = score.ToString()
and after Application.LoadLevel("Menu");
So it should display the latest score.
Answer by jcv8000 · Apr 03, 2015 at 08:08 PM
At the end of the game before it switches to the menu scene, you could use
PlayerPrefs.SetInt("Score", score);
And in the menu scene when you display the score use
text.text = PlayerPrefs.GetInt("Score");
or
text.text = PlayerPrefs.GetInt("Score").ToString();
I think they both work.
Your answer
Follow this Question
Related Questions
Carry over texts from one scene to another? 1 Answer
Score system 1 Answer
Scoring System between two different scene? 2 Answers
Why won't the Score reset? 2 Answers
Scene keeps reloading, can't keep score 3 Answers