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

PlayerPrefs Error?

Discussion in 'Scripting' started by Zypher, Jun 14, 2014.

  1. Zypher

    Zypher

    Joined:
    Apr 7, 2014
    Posts:
    36
    This is my attempt:

    Code (JavaScript):
    1. function Update () {
    2.     if(transform.position.y < -2.6){
    3.         GameOver();
    4.     }
    5. }
    6.  
    7. function GameOver(){
    8.     //save highscore
    9.     if(currentScore > PlayerPrefs.GetInt("Score"))
    10.     {
    11.         PlayerPrefs.SetInt("Score", currentScore);
    12.     }
    13.     Application.LoadLevel("Game Over");
    14. }
    I keep receiving an error that states:
    Assets/GameOver.js(11,37): BCE0005: Unknown identifier: 'currentScore'.

    What am I doing wrong?

    I built the PlayerPrefs based on my score script:

    Code (JavaScript):
    1. #pragma strict
    2. static var currentScore : int = 0;
    3.  
    4. var offsetY : float = 40;
    5. var sizeX : float = 200;
    6. var sizeY : float = 20;
    7. function Awake()
    8. {
    9.       currentScore = 0;
    10. }
    11. function OnGUI () {
    12.     GUI.color = Color.yellow;
    13.     GUI.Box (new Rect (Screen.width/2-sizeX/2, offsetY, sizeX, sizeY), "" + currentScore);
    14. }
    Code (JavaScript):
    1.        GameMaster.currentScore += 1;
    2.  
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    You need to use GameMaster.currentScore
    Code (csharp):
    1.  
    2. if (GameMaster.currentScore > PlayerPrefs.GetInt ("Score")) {
    3.     PlayerPrefs.SetInt ("Score", GameMaster.currentScore);
    4. }
    5.  
     
  3. Zypher

    Zypher

    Joined:
    Apr 7, 2014
    Posts:
    36
    The errors have gone, thank you.

    Will I need to create a GUI so that the best score will show at the Game Over Scene?
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    It would be quite difficult to show information without a GUI.
     
  5. Zypher

    Zypher

    Joined:
    Apr 7, 2014
    Posts:
    36
    I figured.

    I took the PlayerPref out of the Game Over function, and created a separate script using function awake. Do I just rap the entire function awake within a OnGUI or would it be much easier to use unity's GUItext?