Quantcast
Channel: Questions in topic: "playerprefs"
Viewing all articles
Browse latest Browse all 1333

How To Add PlayerPrefs Scores?

$
0
0
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(); } 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); } }

Viewing all articles
Browse latest Browse all 1333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>