I managed to figure it out myself and it seemed to work best for the script I was using.
How to save highscores
I am trying to make a system that will save your highscore but It does not seem to work. To do this I am trying to use variables in 2 different scripts. The problem is that my float "previous" is constantly setting it self to my float count and I only want it to set it once.
first script: using UnityEngine; using UnityEngine.UI; using System.Collections;
public class MainScript : MonoBehaviour {
public Text CountText;
public static int count;
public GameObject pipeObject;
public GameObject birdObject;
public float pipeHole;
public static float previous;
// Use this for initialization
void Awake(){
previous = count;
}
void Start () {
PlayerPrefs.DeleteAll ();
count = PlayerPrefs.GetInt ("High Score", 0);
SetCountText ();
Instantiate (birdObject);
InvokeRepeating ("CreateObstacle", 0f, 1.5f);
}
void CreateObstacle(){
float randomPos = 4f - (4f - 0.8f - pipeHole) * Random.value;
GameObject upperPipe = Instantiate (pipeObject);
upperPipe.transform.position = new Vector2 (4f, randomPos);
GameObject lowerPipe = Instantiate (pipeObject);
lowerPipe.transform.position = new Vector2 (4f, randomPos - pipeHole - 4.8f);
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire1")) {
count = count + 1;
PlayerPrefs.SetInt ("High Score", MainScript.count);
SetCountText ();
print (count);
}
}
void SetCountText(){
CountText.text = "Flaps: " + MainScript.count.ToString ();
}
}
second script: using UnityEngine; using UnityEngine.UI; using System.Collections; using UnityEngine.SceneManagement;
public class LevelManager : MonoBehaviour {
public Text score;
public Text highScore;
public void LoadLevel (string name){
SceneManager.LoadScene (name);
}
// Use this for initialization
void Start () {
score.text = "Score:" + PlayerPrefs.GetInt ("High Score").ToString ();
if(PlayerPrefs.GetInt("High Score") >= MainScript.previous){
highScore.text = "highScore:" + PlayerPrefs.GetInt ("High Score").ToString ();
}
}
// Update is called once per frame
void Update () {
}
}
Answer by Philosophbot · Mar 24, 2017 at 03:17 PM
I managed to figure it out here is what I did I added a new public static int to my first script that only set itself to count when count was greater than itself. I also deleted my public static int called previous and removed it from all my code. public class MainScript : MonoBehaviour {
public Text CountText;
public static int count;
public GameObject pipeObject;
public GameObject birdObject;
public float pipeHole;
public static int previousCount;
// Use this for initialization
void Awake(){
}
void Start () {
count = 0;
//count = PlayerPrefs.GetInt ("High Score", 0);
SetCountText ();
Instantiate (birdObject);
InvokeRepeating ("CreateObstacle", 0f, 1.5f);
}
void CreateObstacle(){
float randomPos = 4f - (4f - 0.8f - pipeHole) * Random.value;
GameObject upperPipe = Instantiate (pipeObject);
upperPipe.transform.position = new Vector2 (4f, randomPos);
GameObject lowerPipe = Instantiate (pipeObject);
lowerPipe.transform.position = new Vector2 (4f, randomPos - pipeHole - 4.8f);
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire1")) {
count = count + 1;
PlayerPrefs.SetInt ("High Score", MainScript.count);
SetCountText ();
print (count);
}
if (count > previousCount) {
previousCount = count;
}
}
void SetCountText(){
CountText.text = "Flaps: " + MainScript.count.ToString ();
}
}
I would also like to note that I noted out 2 second line it the start function because it is a useless line of code and it is very important to delete it.
here is what I edited in my second page of code:
public class LevelManager : MonoBehaviour {
public Text score;
public Text highScore;
public void LoadLevel (string name){
SceneManager.LoadScene (name);
}
// Use this for initialization
void Start () {
MainScript.count = 0;
score.text = "Score:" + PlayerPrefs.GetInt ("High Score").ToString ();
if(PlayerPrefs.GetInt("High Score") >= MainScript.count){
highScore.text = "highScore:" + MainScript.previousCount;
}
}
// Update is called once per frame
void Update () {
}
}
here I replaced values in the start function
Follow this Question
Related Questions
Double tap to reduce score by 2 points 0 Answers
I have no Idea why my highscore Script isn't working ... 0 Answers
Game progress restart / Prestige in Unity 0 Answers
I want to spawn objects probabilistically. 0 Answers
how to create a score multiplier? 0 Answers