- Home /
Persistent Simple Score
I have a simple problem and don't know the right approach; I am using this to control what level to load based on the score:
var Score: int = 0;
function Update () {
if(Score == 500)
{
Application.LoadLevel("Survival_Level_1");
}
the problem is I don't know how to read the actual score without loading a level containing the score object. Also, I am not sure if using this:
PlayerPrefs.SetInt("SCORE", PlayerPrefs.GetInt("SCORE")+1);
SCORE being the object with the score script attached - will actually save my score anywhere. Any advice/references are appreciated.
I think if I put the score object in the main menu and don't destroy it throughout the game would give me access to it at all times. However, the playerprefs kill me cause nothing seems to happen :)
Answer by lhk · May 02, 2011 at 02:42 PM
If there is only one instance of this script it might be easiest to make score static. Here's some c# code :
public static in score=0;
void Update(){ if(Score==500)Application.LoadLevel("Survival_Level_1"); }
Let's say you named the script "score.cs", you can now use this from any other script like this
score.score=500;
Note that there is not one instance of the variable score for every "instance of" the script but just one at all. Therefore you don't have to deal with things like broadcastMessage or getComponent.
Your answer
![](https://koobas.hobune.stream/wayback/20220613130900im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How many different scripts do I need to write to create a highscore system? 1 Answer
saving highscores 1 Answer
keep adding to playerprefs 1 Answer
Saving Score In Player Prefs? 3 Answers
Recognising high score when updated with a graphic 0 Answers