Hey recently i started game development using Unity. I'm learning by watching examples from youtube.' I'm trying make a clone of flappy bird. I wanted to add highscore to my game Here's the script c# Code (csharp): using UnityEngine; public class Generate : MonoBehaviour { public GameObject rocks; int score = 0; void Start() { InvokeRepeating("CreateObstacle", 1f, 1.5f); } void OnGUI () { GUI.color = Color.black; GUILayout.Label(" Score: " + score.ToString()); } void CreateObstacle() { Instantiate(rocks); score++; } }
I'm talking about high score/best score not current score. I mean below the current score i want to show the best score by far.
Create a highestScore variable, then check it everytime you add. Save it using PlayerPrefs. Code (CSharp): public float highestScore; void Start() { highestScore = PlayerPrefs.GetFloat("Highscore"); // Load InvokeRepeating("CreateObstacle", 1f, 1.5f); } void CreateObstacle() { Instantiate(rocks); score++; if (score > highestScore) // Check if current is highest ever { PlayerPrefs.SetFloat("Highscore", score); // Save } }
It's because your public int highestScore is outside of the class. It should be inside the class. Something like this: Code (CSharp): public class generate: monobehaviour { public GameObject rocks; public int highestScore; int score = 0; //etc etc.. }
i did that before but i was getting 2 errors Assets/Scripts/Generate.cs(15,17): error CS0266: Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?) and Assets/Scripts/Generate.cs(33,13): error CS0029: Cannot implicitly convert type `void' to `int' here is the full code Code (csharp): using UnityEngine; public class Generate : MonoBehaviour { public GameObject rocks; public int highestScore; int score = 0; // Use this for initialization void Start() { highestScore = PlayerPrefs.GetFloat("Highscore"); // Load InvokeRepeating("CreateObstacle", 1f, 1.5f); } // Update is called once per frame void OnGUI () { GUI.color = Color.black; GUILayout.Label(" Score: " + score.ToString()); } void CreateObstacle() { Instantiate(rocks); score++; if (score > highestScore) // Check if current is highest ever { highestScore = PlayerPrefs.SetFloat("Highscore", score); // Save } } }
Code (csharp): public int highestScore; ... highestScore =PlayerPrefs.GetFloat(//...//); highScore is an int, and you're trying to use SetFloat... I suspect stardog meant for highestScore to be a float, not an int.
so what should i do? i changed public int highestScore; to public float highestScore; now i'm only getting this error Assets/Scripts/Generate.cs(33,13): error CS0029: Cannot implicitly convert type `void' to `float'
ok, line 33; are you trying to set the playerpref value, or retrieve the playerpref value? if you lookup the functions on the scripting api (automatically links to that in these forums, just click on them) you'll see how each of those functions are supposed to be used. At the moment you appear to be trying to use the "set" function to retrieve the value, which is wrong.
I messed up the code like LeftyRighty said, check out the new one - http://forum.unity3d.com/threads/need-help-adding-high-scores.398153/#post-2599350 I used int by mistake, and SetFloat is void, so no need to assign it to the highestScore.