My problem is i dont know how to save a highscore i have looked at tutorials but none of them work and i cant figure it out so if one of you could help it would much be appreciated here is my script
using UnityEngine;
public class Generate : MonoBehaviour
{
public GameObject rings;
int score = 0;
// Use this for initialization
void Start()
{
InvokeRepeating("CreateObstacle", 1f, 1.5f);
}
// Update is called once per frame
void OnGUI ()
{
GUI.color = Color.red;
GUILayout.Label(" Score: " + score.ToString());
}
void CreateObstacle()
{
Instantiate(rings);
score++;
}
PlayerPrefs.SetInt may have a slight performance issue if called every frame ( especially on mobiles). So try to save at the end of the scene, on exit or at some checkpoint.
You can do the following as soon as your game/level/round is finished. It’ll save the highscore to the PlayerPrefs:
// loads the value for "highscore", returns the default 0 if it doesn't exist
int highscore = PlayerPrefs.GetInt("highscore", 0);
if(highscore < score)
{
// saves the value of "score" into the highscore field
PlayerPrefs.SetInt("highscore", score);
}