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

Whats wrong with this code, trying to save a high score using playerprefs

Discussion in 'Scripting' started by MIST0, Jun 27, 2014.

  1. MIST0

    MIST0

    Joined:
    Apr 26, 2014
    Posts:
    53
    Whatss wrong with this code, trying to save a high score using playerprefs

    using UnityEngine;
    using System.Collections;
    public class Score : MonoBehaviour {
    public GUIText countText;
    public GUIText endText;
    public int score;

    void Start()
    {
    score = 0;
    endText.text = "";
    countText.text = "Score";
    }
    void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.tag == "PickUp") {
    score = score + 1;
    SetCountText ();
    }
    if (other.gameObject.name == "Pick Up2") {
    score = score + 4;
    SetCountText ();
    }
    if (other.gameObject.name == "Pick Up3") {
    score = score + 9;
    SetCountText ();
    }
    if (other.gameObject.name == "Pick Up4") {
    score = score + 19;
    SetCountText ();
    }
    if (other.gameObject.tag == "Player") {
    endText.text = "Final Score " + score.ToString ();
    }

    }
    void SetCountText()
    {
    countText.text = "Score " + score.ToString();
    }
    void Gameover()
    {

    if (score > PlayerPrefs.GetInt ("HighScore"))
    {
    PlayerPrefs.SetInt ("HighScore", score);
    }
    }

    void OnGUI ()
    {

    {
    if (GUILayout.Button("Restart", GUILayout.Height(50), GUILayout.Width(100)))

    Application.LoadLevel (1);
    }
    {
    GUI.Label (new Rect(300, 0, 100, 50),"High Score " + score);
    }

    }
    }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    well firstly you're missing [code ] [/code ] tags on the code you've pasted into the forums :p

    is it throwing an error or are you just not seeing the high score on the guilabel? because you don't get the high score value to display on the label, you're using the local (I assume latest game) score.
     
  3. MIST0

    MIST0

    Joined:
    Apr 26, 2014
    Posts:
    53
    There's no error, the high score counts up during the game just like that normal score counter, however when I restart the game using the GUI button it resets the score back to 0.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. score = 0;
    3. ...
    4. GUI.Label (new Rect(300, 0, 100, 50),"High Score " + score);
    5.  
    you are using "score" ... you want to retrieve the highscore from playerprefs to display here...
     
  5. MIST0

    MIST0

    Joined:
    Apr 26, 2014
    Posts:
    53
    Ok thanks but how lol.
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you do that in the if

    Code (csharp):
    1.  
    2. PlayerPrefs.GetInt ("HighScore")
    3.  
     
  7. MIST0

    MIST0

    Joined:
    Apr 26, 2014
    Posts:
    53
    Anyone give me any examples