The question is answered, right answer was accepted
Highscore not working
Hi, im working on a game named sqwitch. Its a game where you'd have to match the colors of the box to the colors of the tiles dropping down. Im wanting to implement a highscore system for a while. I thought of using playerprefs. But I'm kind of a noob. Its not working :(
I have 4 scripts checking collision 1 for each color, there points are given out. exactly the same except some tags or names. Here is one :
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class OCOLLISION : MonoBehaviour { public GameObject player;
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag != "Oranje")
{
if (ScoreScript.ScoreValue > PlayerPrefs.GetInt("Highscore", ScoreScript.ScoreValue))
{
PlayerPrefs.SetInt("Highscore", ScoreScript.ScoreValue);
PlayerPrefs.Save();
}
PlayerWacht();
gameObject.active = false;
ScoreScript.ScoreValue = 0;
}
if (other.gameObject.tag == "Oranje")
{
Destroy(gameObject);
ScoreScript.ScoreValue += 1;
}
}
void playerdood()
{
ScoreScript.ScoreValue = 0;
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
}
public void PlayerWacht()
{
Invoke("playerdood", 2);
}
}
The score is working fine here is my script for the score:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class ScoreScript : MonoBehaviour { public static int ScoreValue = 0; public Text Score;
void Start() { Score = GetComponent(); }
void Update() { Score.text = "" + ScoreValue; } }
this script is attached to a text object inside my game. My script for highscores in the start menu scene:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class HighScore : MonoBehaviour { public Text Highscore;
void Start()
{
Highscore.text = PlayerPrefs.GetInt("Highscore").ToString();
}
void Update()
{
}
void OnApplicationQuit()
{
PlayerPrefs.Save();
}
}
I just cant figure out why this isn't working. Im clearly doing something wrong. This game is targeting android. But the playerpref doesn't work in the editor. Any help would be appreciated :)
Thanks in advance Jesse.
Follow this Question
Related Questions
PlayerPrefs Not working. How Can I fix it? 1 Answer
High-score Saving Issue 2 Answers
how do i store highscore locally C# (SIMPLE) 3 Answers
Highscore Not Updating Upon Death 1 Answer