Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

My logic is broken can i have help?

Discussion in 'Scripting' started by PvTGreg, Aug 25, 2014.

  1. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    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?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Counter : MonoBehaviour {
    5.    
    6.     public int CurScore;
    7.     private int HighScore;
    8.     private int HighScore2;
    9.     private int HighScore3;
    10.     private int HighScore4;
    11.     private int HighScore5;
    12.  
    13.     void Start ()
    14.     {  
    15.         HighScore = PlayerPrefs.GetInt("HighScore");
    16.         HighScore2 = PlayerPrefs.GetInt ("HighScore2");
    17.         HighScore3 = PlayerPrefs.GetInt ("HighScore3");
    18.         HighScore4 = PlayerPrefs.GetInt ("HighScore4");
    19.         InvokeRepeating("Points", 2, 0.3F);
    20.     }
    21.    
    22.    
    23.     // Update is called once per frame
    24.     void Points ()
    25.     {
    26.         CurScore += 1;
    27.    
    28.     }
    29.     void Update () {
    30.  
    31.         //if (Show.GetComponent(Respawn.ShowGUI) );
    32.         //{
    33.  
    34.  
    35.         // replace score with new highscore
    36.         if (CurScore > HighScore)
    37.         {
    38.             // Set the highscore to our current score
    39.             HighScore = CurScore;
    40.             // savign highscore
    41.             PlayerPrefs.SetInt("HighScore", HighScore);
    42.         }
    43.  
    44.        
    45.         if(CurScore < HighScore)
    46.         {
    47.             HighScore2 = CurScore;
    48.             PlayerPrefs.SetInt("HighScore2", HighScore2);
    49.  
    50.         }
    51.        
    52.         if(CurScore < HighScore2)
    53.         {
    54.             HighScore3 = CurScore;
    55.             PlayerPrefs.SetInt("HighScore3", HighScore3);
    56.  
    57.         }
    58.        
    59.         if(CurScore < HighScore3)
    60.         {
    61.             HighScore4 = CurScore;
    62.             PlayerPrefs.SetInt("HighScore4", HighScore4);
    63.  
    64.         }
    65.     //}
    66.     }
    67.     void OnGUI()
    68.     {
    69.         HighScore = PlayerPrefs.GetInt("HighScore");
    70.         HighScore2 = PlayerPrefs.GetInt("HighScore2");
    71.         HighScore3 = PlayerPrefs.GetInt("HighScore3");
    72.         HighScore4 = PlayerPrefs.GetInt("HighScore4");
    73.         GUI.Box(new Rect(10,10,100,30),"Score:"+CurScore.ToString());
    74.         GUI.Box(new Rect(10,50,100,30),"HighScore:"+HighScore.ToString());
    75.         GUI.Box(new Rect(10,90,100,30),"HighScore2:"+HighScore2.ToString());
    76.         GUI.Box(new Rect(10,120,100,30),"HighScore3:"+HighScore3.ToString());
    77.         GUI.Box(new Rect(10,160,100,30),"HighScore4:"+HighScore4.ToString());
    78.        
    79.     }
    80. }
    81.  
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    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.
     
  3. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    Hi would it be possible for you to show me and example? as i dont see how to would make a differance
     
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Something like this:

    Code (csharp):
    1.  
    2. function CheckScore()
    3. {
    4.         if (CurScore >= HighScore)
    5.         {
    6.             // Set the highscore to our current score
    7.             HighScore = CurScore;
    8.             // savign highscore
    9.             PlayerPrefs.SetInt("HighScore", HighScore);
    10.         }
    11.         else //(CurScore < HighScore)
    12.         {
    13.                if(CurScore >= HighScore2)
    14.                {
    15.                           HighScore2 = CurScore;
    16.                           PlayerPrefs.SetInt("HighScore2", HighScore2);
    17.                }
    18.          }
    19. }
    20.  
     
  5. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    awsome it works but how do i go about adding more elses?
     
  6. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    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.
     
  7. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    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
     
  8. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    Code (CSharp):
    1. if (CurScore >= HighScore)
    2.         {
    3.             // Set the highscore to our current score
    4.             HighScore = CurScore;
    5.             // savign highscore
    6.             PlayerPrefs.SetInt("HighScore", HighScore);
    7.         }
    8.         else
    9.         {
    10.             if(CurScore >= HighScore2)
    11.             {
    12.                 HighScore2 = CurScore;
    13.                 PlayerPrefs.SetInt("HighScore2", HighScore2);
    14.             }
    15.         }
    16.  
    17.         if (CurScore >= HighScore3 && CurScore < HighScore2  )
    18.         {
    19.             // Set the highscore to our current score
    20.             HighScore3 = CurScore;
    21.             // savign highscore
    22.             PlayerPrefs.SetInt("HighScore3", HighScore3);
    23.         }

    managed to get it to work with this
     
  9. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    That is bad, they all told you wrong. Utilize IF ELSE-IF clause no need to nest when you do not need to.

    Code (CSharp):
    1.         // replace score with new highscore
    2.         if (CurScore > HighScore)
    3.         {
    4.             // Set the highscore to our current score
    5.             HighScore = CurScore;
    6.             // savign highscore
    7.             PlayerPrefs.SetInt("HighScore", HighScore);
    8.         } else if(CurScore < HighScore)
    9.         {
    10.             HighScore2 = CurScore;
    11.             PlayerPrefs.SetInt("HighScore2", HighScore2);
    12.            
    13.         } else if(CurScore < HighScore2)
    14.         {
    15.             HighScore3 = CurScore;
    16.             PlayerPrefs.SetInt("HighScore3", HighScore3);
    17.            
    18.         } else if(CurScore < HighScore3)
    19.         {
    20.             HighScore4 = CurScore;
    21.             PlayerPrefs.SetInt("HighScore4", HighScore4);
    22.            
    23.         } else {
    24.             // if no if was triggered
    25.         }
    Only one condition will get triggered.
     
  10. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    your code does not work it keeps reseting score 2
     
  11. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Because your logic is off. The IF-ELSE-IF statement structure is correct but, the way your conditions are must be changed.