Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

The Follwing Script is not working, I want it to work to save the score and display.

Discussion in 'Scripting' started by sthhere11, Mar 30, 2014.

  1. sthhere11

    sthhere11

    Joined:
    Jun 28, 2013
    Posts:
    29
    Please tell me the solution, I am saving the score using PlayerPrefs then loading, but not working.



    //This script is attached to the main player, for all the player logic (movement) as well as some Gui Stuff
    using UnityEngine;
    using System.Collections;

    public class PlayerController : MonoBehaviour
    {
    public AudioClip pointSound;
    public AudioClip failSound;
    public AudioClip touchSound;
    public Texture2D tapImage;

    // Use this for initialization
    void Start ()
    {
    isStartButtonPressed = false;
    Time.timeScale = 0.0f;
    restartMenu = false;
    Camera.main.GetComponent<BlurEffect> ().enabled = false;
    highScore = PlayerPrefs.GetInt ("highScore");
    //score = PlayerPrefs.GetInt ("score");


    }

    // Update is called once per frame
    void Update ()
    {
    updateScore ();
    if (!isInView ()) {
    restartGame ();
    }
    if (Input.anyKeyDown) {
    move ();
    }

    if (score > highScore) {
    // Set the highscore to our current score
    highScore = score;
    // Now save the score as our new highscore
    PlayerPrefs.SetInt ("highScore", highScore);
    }

    // Debug.Log ("CurrentScore" + score);
    //Debug.Log ("highScore" + highScore);
    }

    //To check whether the player is in the scene
    private bool isInView ()
    {
    Vector3 port = Camera.main.WorldToViewportPoint (transform.position);
    if ((port.x < 1) (port.x > 0) (port.y < 1) (port.y > 0) port.z > 0) {
    return true;
    } else {
    return false;
    }

    }

    private bool isStartButtonPressed;
    public GUIText scoreLabel;
    public static int score;
    private int highScore;
    private bool restartMenu;
    public GUISkin skin;

    void OnGUI ()
    {
    if (!isStartButtonPressed) {
    GUI.skin = skin;

    GUI.Label (new Rect (Screen.width / 2 - 40, Screen.height / 2 - 25, 200, 100), "Tap to pass ");
    GUI.DrawTexture (new Rect (Screen.width / 2 - 65, Screen.height / 1.9f, 130, 100), tapImage);
    if (Input.anyKeyDown) {
    Time.timeScale = 1.0f;
    isStartButtonPressed = true;
    }
    }

    GUI.Box (new Rect (Screen.width / 4 - 65, Screen.height / 2 - 11, 130, 22), "CurrentScore " + score);
    GUI.Box (new Rect (Screen.width / 1.2f - 65, Screen.height / 2 - 11, 130, 22), "highScore " + highScore);

    GUI.skin = skin;

    if (restartMenu == true) {
    Camera.main.GetComponent<BlurEffect> ().enabled = true;
    Rect currentGameOver = new Rect (Screen.width / 2.8f, Screen.height / 3, 400, 200);
    GUI.Box (currentGameOver, "Game Over", ("Game Over"));
    GUI.Label (new Rect (currentGameOver.x + 15f, currentGameOver.y + 50f, currentGameOver.width * .5f, currentGameOver.height * .25f), "Score: " + score.ToString ());
    GUI.Label (new Rect (currentGameOver.x + 15f, currentGameOver.y + 70f, currentGameOver.width * .5f, currentGameOver.height * .25f), "High score: " + highScore.ToString ());
    if (GUI.Button (new Rect (currentGameOver.x + (currentGameOver.width - 150), currentGameOver.y + (currentGameOver.height - 80), 130, 60), "", skin.GetStyle ("Play"))) {
    CameraController.range = Random.Range (1, 4);
    // Debug.Log (CameraController.range);
    Application.LoadLevel (Application.loadedLevelName);
    highScore = PlayerPrefs.GetInt ("highScore");
    isStartButtonPressed = false;
    }

    }
    }

    private void move ()
    {
    rigidbody.velocity = new Vector3 (0, 0, 0);
    rigidbody.AddForce (new Vector3 (275, 200, 0), ForceMode.Force);
    audio.PlayOneShot(touchSound);

    }

    void OnTriggerEnter (Collider other)
    {
    animation.Play ("IceAnimation");
    audio.PlayOneShot (failSound);
    StartCoroutine (MyCoroutine ());
    }

    IEnumerator MyCoroutine ()
    {
    yield return new WaitForSeconds(1.8f);
    restartGame ();
    }

    private void restartGame ()
    {
    PlayerPrefs.SetInt ("currentScore", score);
    Debug.Log("Score" + score);
    PlayerPrefs.SetInt ("highScore", highScore);
    Time.timeScale = 0.0f;
    restartMenu = true;

    }

    private int lastPlayed = -1;

    private void updateScore ()
    {
    int scoreLabelInt = int.Parse (scoreLabel.text);
    float nextBlockPosition = 5 + GenerateWorld.distanceBetweenObjects * scoreLabelInt;
    if ((transform.position.x <= nextBlockPosition + 0.60) (transform.position.x >= nextBlockPosition + 0.5) (lastPlayed != scoreLabelInt))
    {
    audio.PlayOneShot (pointSound);
    lastPlayed = scoreLabelInt;
    }
    int score = (int)(transform.position.x / GenerateWorld.distanceBetweenObjects);
    if (score != (scoreLabelInt) score > 0)
    {
    scoreLabel.text = score.ToString ();
    }


    }

    }
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
  3. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Just making your script easier to read for other people :

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public AudioClip pointSound;
    8.     public AudioClip failSound;
    9.     public AudioClip touchSound;
    10.     public Texture2D tapImage;
    11.  
    12.     // Use this for initialization
    13.     void Start ()
    14.     {
    15.         isStartButtonPressed = false;
    16.         Time.timeScale = 0.0f;
    17.         restartMenu = false;
    18.         Camera.main.GetComponent<BlurEffect> ().enabled = false;
    19.         highScore = PlayerPrefs.GetInt ("highScore");
    20.         //score = PlayerPrefs.GetInt ("score");
    21.  
    22.  
    23.     }
    24.    
    25.     // Update is called once per frame
    26.     void Update ()
    27.     {
    28.         updateScore ();
    29.         if (!isInView ()) {
    30.             restartGame ();
    31.         }
    32.         if (Input.anyKeyDown) {
    33.             move ();
    34.         }
    35.        
    36.         if (score > highScore) {
    37.             // Set the highscore to our current score
    38.             highScore = score;
    39.             // Now save the score as our new highscore
    40.             PlayerPrefs.SetInt ("highScore", highScore);
    41.         }
    42.        
    43. //      Debug.Log ("CurrentScore" + score);
    44.         //Debug.Log ("highScore" + highScore);
    45.     }
    46.    
    47.     //To check whether the player is in the scene
    48.     private bool isInView ()
    49.     {
    50.         Vector3 port = Camera.main.WorldToViewportPoint (transform.position);
    51.         if ((port.x < 1)  (port.x > 0)  (port.y < 1)  (port.y > 0)  port.z > 0) {
    52.             return true;
    53.         } else {
    54.             return false;
    55.         }
    56.        
    57.     }
    58.  
    59.     private bool isStartButtonPressed;
    60.     public GUIText scoreLabel;
    61.     public static int score;
    62.     private int highScore;
    63.     private bool restartMenu;
    64.     public GUISkin skin;
    65.  
    66.     void OnGUI ()
    67.     {
    68.         if (!isStartButtonPressed) {
    69.             GUI.skin = skin;
    70.  
    71.             GUI.Label (new Rect (Screen.width / 2 - 40, Screen.height / 2 - 25, 200, 100), "Tap to pass ");
    72.             GUI.DrawTexture (new Rect (Screen.width / 2 - 65, Screen.height / 1.9f, 130, 100), tapImage);
    73.             if (Input.anyKeyDown) {
    74.                 Time.timeScale = 1.0f;
    75.                 isStartButtonPressed = true;
    76.             }
    77.         }
    78.            
    79.         GUI.Box (new Rect (Screen.width / 4 - 65, Screen.height / 2 - 11, 130, 22), "CurrentScore " + score);
    80.         GUI.Box (new Rect (Screen.width / 1.2f - 65, Screen.height / 2 - 11, 130, 22), "highScore " + highScore);
    81.            
    82.         GUI.skin = skin;
    83.  
    84.         if (restartMenu == true) {
    85.             Camera.main.GetComponent<BlurEffect> ().enabled = true;
    86.             Rect currentGameOver = new Rect (Screen.width / 2.8f, Screen.height / 3, 400, 200);
    87.             GUI.Box (currentGameOver, "Game Over", ("Game Over"));
    88.             GUI.Label (new Rect (currentGameOver.x + 15f, currentGameOver.y + 50f, currentGameOver.width * .5f, currentGameOver.height * .25f), "Score: " + score.ToString ());
    89.             GUI.Label (new Rect (currentGameOver.x + 15f, currentGameOver.y + 70f, currentGameOver.width * .5f, currentGameOver.height * .25f), "High score: " + highScore.ToString ());
    90.             if (GUI.Button (new Rect (currentGameOver.x + (currentGameOver.width - 150), currentGameOver.y + (currentGameOver.height - 80), 130, 60), "", skin.GetStyle ("Play"))) {
    91.                 CameraController.range = Random.Range (1, 4);
    92. //              Debug.Log (CameraController.range);
    93.                 Application.LoadLevel (Application.loadedLevelName);   
    94.                 highScore = PlayerPrefs.GetInt ("highScore");
    95.                 isStartButtonPressed = false;
    96.             }
    97.  
    98.         }
    99.     }
    100.  
    101.     private void move ()
    102.     {
    103.         rigidbody.velocity = new Vector3 (0, 0, 0);
    104.         rigidbody.AddForce (new Vector3 (275, 200, 0), ForceMode.Force);
    105.         audio.PlayOneShot(touchSound);
    106.  
    107.     }
    108.  
    109.     void OnTriggerEnter (Collider other)
    110.     {
    111.         animation.Play ("IceAnimation");
    112.         audio.PlayOneShot (failSound);
    113.         StartCoroutine (MyCoroutine ());       
    114.     }
    115.    
    116.     IEnumerator MyCoroutine ()
    117.     {
    118.         yield return new WaitForSeconds(1.8f);
    119.         restartGame ();
    120.     }
    121.  
    122.     private void restartGame ()
    123.     {
    124.         PlayerPrefs.SetInt ("currentScore", score);
    125.         Debug.Log("Score" + score);
    126.         PlayerPrefs.SetInt ("highScore", highScore);
    127.         Time.timeScale = 0.0f;
    128.         restartMenu = true;
    129.  
    130.     }
    131.  
    132.     private int lastPlayed = -1;
    133.  
    134.     private void updateScore ()
    135.     {
    136.         int scoreLabelInt = int.Parse (scoreLabel.text);
    137.         float nextBlockPosition = 5 + GenerateWorld.distanceBetweenObjects * scoreLabelInt;
    138.         if ((transform.position.x <= nextBlockPosition + 0.60)  (transform.position.x >= nextBlockPosition + 0.5)  (lastPlayed != scoreLabelInt))
    139.         {
    140.             audio.PlayOneShot (pointSound);
    141.             lastPlayed = scoreLabelInt;
    142.         }
    143.         int score = (int)(transform.position.x / GenerateWorld.distanceBetweenObjects);
    144.         if (score != (scoreLabelInt)  score > 0)
    145.         {
    146.             scoreLabel.text = score.ToString ();
    147.         }
    148.  
    149.  
    150.     }
    151.  
    152. }
    153.  
    154.