- Home /
How To Add PlayerPrefs Scores?
Hello again. This might be a simple question, but how can I add a saved score from one scene to another? So like let's say I have a score of 300, and I do something to load a different scene, played it, got 200 more points, headed back to the first scene and now the score is 500(300 + 200 = 500). How would I add those 200 points to the 300 score? Think of it as a bonus/special stage adding on to your regular score. Please I hope you can help me. My scoring script for scene1 that's attached to the Player:
public class ScoringScript : MonoBehaviour {
float playerScore = 0;
public float guiPlacementY1;
public float guiPlacementX1;
Stage1 player;
void Start()
{
player = GetComponent<Stage1>();
}
public void IncreaseScore(int amount)
{
playerScore += amount;
}
public void DecreaseScore(int amount)
{
playerScore -= amount;
}
void OnDisable()
{
PlayerPrefs.SetInt ("Score", (int)(playerScore * 5));
PlayerPrefs.SetString("PlayerName", "High Score");
PlayerPrefs.Save ();
Debug.Log("Saved");
}
void OnGUI()
{
GUI.Label (new Rect(Screen.width * guiPlacementX1, Screen.height * guiPlacementY1, Screen.width * .5f, Screen.height * .4f), "Score: " + (int)(playerScore * 5));
}
}
And the script that displays the score:
public class GameOver : MonoBehaviour {
int score = 0;
int highscore = 0;
public float guiPlacementY1;
public float guiPlacementY2;
public float guiPlacementX1;
public float guiPlacementX2;
void Start () {
score = PlayerPrefs.GetInt ("Score");
highscore = PlayerPrefs.GetInt ("High Score");
Debug.Log ("M");
if (score > highscore) { //when player dies set highscore = to that score
highscore = score;
PlayerPrefs.SetInt ("High Score", highscore);
Debug.Log("ik");
}
}
void OnGUI()
{
GUI.Label (new Rect (Screen.width * guiPlacementX1, Screen.height * guiPlacementY1, Screen.width * .5f, Screen.height * .4f), "GAME OVER");
GUI.Label (new Rect (Screen.width * guiPlacementX2, Screen.height * guiPlacementY2, Screen.width * .5f, Screen.height * .4f), "Score: " + score);
}
}
Here you can use static data member and also you can access it in other script so you can easily update your score in multiple scene.
public static int Score;
In Othes Script you can access it with class name
So I make my score integer in my GameOver script public static? If so, after that how do I access it correctly?
I posted an answer for your PlayerPrefs question, but if you want to use a static method, I would personally just have a GameObject to manage all persistence so it could call DontDestroyOnLoad(gameObject); and hold all of your stats.
Answer by KMKxJOEY1 · Sep 23, 2014 at 06:12 PM
PlayerPrefs.SetInt("Score", PlayerPrefs.GetInt("Score",0) + score);
Does that go in the scoring script? The scoring script is the script I use for the bonus stage for points. The script I use for Stage 1(the regular stage) is the same script but with a different name. I only use the Game over script for Stage 1.
So does it or not? Sorry to bother but I need to know.
EDIT: I still haven't found out what to do. Does anyone have some tutorials I can watch/read?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Player lives script help 1 Answer
Weird GUILayout Scrollview Behavior 0 Answers
Changing Axis to Buttons 2 Answers
Need help calling a script to another keep getting errors 1 Answer