Search Unity

Need help adding high scores

Discussion in 'Scripting' started by yesyesok, Apr 17, 2016.

  1. yesyesok

    yesyesok

    Joined:
    Apr 17, 2016
    Posts:
    13
    Hey recently i started game development using Unity. I'm learning by watching examples from youtube.'
    I'm trying make a clone of flappy bird.

    I wanted to add highscore to my game

    Here's the script


    c#
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class Generate : MonoBehaviour
    5. {
    6.     public GameObject rocks;
    7.     int score = 0;
    8.    
    9.     void Start()
    10.     {
    11.         InvokeRepeating("CreateObstacle", 1f, 1.5f);
    12.     }
    13.    
    14.    
    15.     void OnGUI ()
    16.     {
    17.         GUI.color = Color.black;
    18.         GUILayout.Label(" Score: " + score.ToString());
    19.     }
    20.    
    21.     void CreateObstacle()
    22.     {
    23.         Instantiate(rocks);
    24.         score++;
    25.     }
    26. }
     
  2. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    It looks like it's already working.
     
  3. yesyesok

    yesyesok

    Joined:
    Apr 17, 2016
    Posts:
    13
    I'm talking about high score/best score not current score.


    I mean below the current score i want to show the best score by far.
     
  4. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    Create a highestScore variable, then check it everytime you add. Save it using PlayerPrefs.

    Code (CSharp):
    1.     public float highestScore;
    2.  
    3.     void Start()
    4.     {
    5.         highestScore = PlayerPrefs.GetFloat("Highscore"); // Load
    6.  
    7.         InvokeRepeating("CreateObstacle", 1f, 1.5f);
    8.     }
    9.  
    10.     void CreateObstacle()
    11.     {
    12.         Instantiate(rocks);
    13.         score++;
    14.  
    15.         if (score > highestScore) // Check if current is highest ever
    16.         {
    17.             PlayerPrefs.SetFloat("Highscore", score); // Save
    18.         }
    19.     }
     
    Last edited: Apr 18, 2016
    yesyesok and TaleOf4Gamers like this.
  5. Djinnet

    Djinnet

    Joined:
    Aug 27, 2014
    Posts:
    4
    It's because your public int highestScore is outside of the class. It should be inside the class. Something like this:
    Code (CSharp):
    1. public class generate: monobehaviour
    2. {
    3.    public GameObject rocks;
    4.    public int highestScore;
    5.    int score = 0;
    6.  
    7. //etc etc..
    8. }
     
  6. yesyesok

    yesyesok

    Joined:
    Apr 17, 2016
    Posts:
    13
    i did that before but i was getting 2 errors

    Assets/Scripts/Generate.cs(15,17): error CS0266: Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?)

    and

    Assets/Scripts/Generate.cs(33,13): error CS0029: Cannot implicitly convert type `void' to `int'



    here is the full code

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class Generate : MonoBehaviour
    4.  
    5.  
    6. {
    7.    
    8.     public GameObject rocks;
    9.     public int highestScore;
    10.     int score = 0;
    11.    
    12.     // Use this for initialization
    13.     void Start()
    14.     {
    15.         highestScore = PlayerPrefs.GetFloat("Highscore"); // Load
    16.         InvokeRepeating("CreateObstacle", 1f, 1.5f);
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void OnGUI ()
    21.     {
    22.         GUI.color = Color.black;
    23.         GUILayout.Label(" Score: " + score.ToString());
    24.     }
    25.    
    26.     void CreateObstacle()
    27.     {
    28.         Instantiate(rocks);
    29.         score++;
    30.        
    31.         if (score > highestScore) // Check if current is highest ever
    32.         {
    33.             highestScore = PlayerPrefs.SetFloat("Highscore", score); // Save
    34.         }
    35.     }
    36. }
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. public int highestScore;
    3. ...
    4. highestScore =PlayerPrefs.GetFloat(//...//);
    5.  
    highScore is an int, and you're trying to use SetFloat... I suspect stardog meant for highestScore to be a float, not an int.
     
  8. yesyesok

    yesyesok

    Joined:
    Apr 17, 2016
    Posts:
    13
    so what should i do?

    i changed
    public int highestScore;

    to

    public float highestScore;


    now i'm only getting this error
    Assets/Scripts/Generate.cs(33,13): error CS0029: Cannot implicitly convert type `void' to `float'
     
    Last edited: Apr 18, 2016
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ok, line 33; are you trying to set the playerpref value, or retrieve the playerpref value? if you lookup the functions on the scripting api (automatically links to that in these forums, just click on them) you'll see how each of those functions are supposed to be used. At the moment you appear to be trying to use the "set" function to retrieve the value, which is wrong.
     
  10. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
  11. yesyesok

    yesyesok

    Joined:
    Apr 17, 2016
    Posts:
    13
    Last edited: Apr 21, 2016