Hi am in need of help i can see that my logic is broken but i have no idea what do to do to solve it
i am trying to create a basic high-score system using if statements to keep it simple and easy to read
it takes in the score and sets it as a highscore then if its less highscore 2 and so on
this is what i have currently but all the time it sets highscore 2 to 0 as it is always gonna be less than highscore how can i get around this?
using UnityEngine;
using System.Collections;
public class Counter : MonoBehaviour {
public int CurScore;
private int HighScore;
private int HighScore2;
private int HighScore3;
private int HighScore4;
private int HighScore5;
void Start ()
{
HighScore = PlayerPrefs.GetInt("HighScore");
HighScore2 = PlayerPrefs.GetInt ("HighScore2");
HighScore3 = PlayerPrefs.GetInt ("HighScore3");
HighScore4 = PlayerPrefs.GetInt ("HighScore4");
InvokeRepeating("Points", 2, 0.3F);
}
// Update is called once per frame
void Points ()
{
CurScore += 1;
}
void Update () {
//if (Show.GetComponent(Respawn.ShowGUI) );
//{
// replace score with new highscore
if (CurScore > HighScore)
{
// Set the highscore to our current score
HighScore = CurScore;
// savign highscore
PlayerPrefs.SetInt("HighScore", HighScore);
}
if(CurScore < HighScore)
{
HighScore2 = CurScore;
PlayerPrefs.SetInt("HighScore2", HighScore2);
}
if(CurScore < HighScore2)
{
HighScore3 = CurScore;
PlayerPrefs.SetInt("HighScore3", HighScore3);
}
if(CurScore < HighScore3)
{
HighScore4 = CurScore;
PlayerPrefs.SetInt("HighScore4", HighScore4);
}
//}
}
void OnGUI()
{
HighScore = PlayerPrefs.GetInt("HighScore");
HighScore2 = PlayerPrefs.GetInt("HighScore2");
HighScore3 = PlayerPrefs.GetInt("HighScore3");
HighScore4 = PlayerPrefs.GetInt("HighScore4");
GUI.Box(new Rect(10,10,100,30),"Score:"+CurScore.ToString());
GUI.Box(new Rect(10,50,100,30),"HighScore:"+HighScore.ToString());
GUI.Box(new Rect(10,90,100,30),"HighScore2:"+HighScore2.ToString());
GUI.Box(new Rect(10,120,100,30),"HighScore3:"+HighScore3.ToString());
GUI.Box(new Rect(10,160,100,30),"HighScore4:"+HighScore4.ToString());
}
}
You need to nest your logic. Every IF statement is a new question for the computer. Right now you are asking four different questions. Try nesting them using “else”.
Also, you should not check the score in Update. You know it only changes when the points go up. What you do then is make another function that you call everytime you call Points(). This makes it a lot cheaper.
Every IF statement can have 1 else. Just add them under the closing bracket of the IF you want to add an else to it. This is basic programming. If you haven’t learned this yet, I would advise to follow some basic courses on the internet, before continuing with other things.
i know about nested ifs and such but i need to use it for 5 scores so this method wont work thats whyi asked if their is a way yo add more than on else