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

Local Highscore save (C#) iOS

Discussion in 'Scripting' started by Foieu, Feb 24, 2015.

  1. Foieu

    Foieu

    Joined:
    Jan 31, 2015
    Posts:
    6
    hello everyone, I have a code, and I was just wondering how I would be able to save a score. The score is calculated based on distance. I have researched this extensively, and could not find anything. Any help is appreciated! Thanks.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Runner : MonoBehaviour {
    4.  
    5.     public static float distanceTraveled;
    6.     private static int boosts;
    7.    
    8.     public float acceleration;
    9.     public Vector3 boostVelocity, jumpVelocity;
    10.     public float gameOverY;
    11.    
    12.     private bool touchingPlatform;
    13.     private Vector3 startPosition;
    14.    
    15.     void Start () {
    16.         GameEventManager.GameStart += GameStart;
    17.         GameEventManager.GameOver += GameOver;
    18.         startPosition = transform.localPosition;
    19.         renderer.enabled = false;
    20.         rigidbody.isKinematic = true;
    21.         enabled = false;
    22.     }
    23.    
    24.     void Update () {
    25.         if(Input.GetMouseButtonDown(0)){
    26.             if(touchingPlatform){
    27.                 rigidbody.AddForce(jumpVelocity, ForceMode.VelocityChange);
    28.                 touchingPlatform = false;
    29.             }
    30.             else if(boosts > 0){
    31.                 rigidbody.AddForce(boostVelocity, ForceMode.VelocityChange);
    32.                 boosts -= 1;
    33.                 GUIManager.SetBoosts(boosts);
    34.             }
    35.         }
    36.         distanceTraveled = transform.localPosition.x;
    37.         GUIManager.SetDistance(distanceTraveled);
    38.        
    39.         if(transform.localPosition.y < gameOverY){
    40.             GameEventManager.TriggerGameOver();
    41.         }
    42.     }
    43.  
    44.     void FixedUpdate () {
    45.         if(touchingPlatform){
    46.             rigidbody.AddForce(acceleration, 0f, 0f, ForceMode.Acceleration);
    47.         }
    48.     }
    49.  
    50.     void OnCollisionEnter () {
    51.         touchingPlatform = true;
    52.     }
    53.  
    54.     void OnCollisionExit () {
    55.         touchingPlatform = false;
    56.     }
    57.  
    58.     private void GameStart () {
    59.         boosts = 0;
    60.         GUIManager.SetBoosts(boosts);
    61.         distanceTraveled = 0f;
    62.         GUIManager.SetDistance(distanceTraveled);
    63.         transform.localPosition = startPosition;
    64.         renderer.enabled = true;
    65.         rigidbody.isKinematic = false;
    66.         enabled = true;
    67.     }
    68.    
    69.     private void GameOver () {
    70.         renderer.enabled = false;
    71.         rigidbody.isKinematic = true;
    72.         enabled = false;
    73.     }
    74.    
    75.     public static void AddBoost(){
    76.         boosts += 1;
    77.         GUIManager.SetBoosts(boosts);
    78.     }
    79. }
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Mycroft likes this.
  3. Foieu

    Foieu

    Joined:
    Jan 31, 2015
    Posts:
    6
    Thanks for replying! Yes, I have come across Player Prefs, but as I making an iOS game, I am not sure how I would integrate that into the iPhone.
     
  4. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    PlayerPrefs works on all OSs and platforms.
     
    Mycroft likes this.
  5. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Here is a simple way to use PlayerPrefs to get a distance high score in your code:

    Code (CSharp):
    1. using UnityEngine;
    2. public class Runner : MonoBehaviour {
    3.     public static float distanceTraveled;
    4.     public static float longestDistanceTraveled;
    5.     private static int boosts;
    6.  
    7.     public float acceleration;
    8.     public Vector3 boostVelocity, jumpVelocity;
    9.     public float gameOverY;
    10.  
    11.     private bool touchingPlatform;
    12.     private Vector3 startPosition;
    13.  
    14.     void Start () {
    15.         GameEventManager.GameStart += GameStart;
    16.         GameEventManager.GameOver += GameOver;
    17.         startPosition = transform.localPosition;
    18.         renderer.enabled = false;
    19.         rigidbody.isKinematic = true;
    20.         enabled = false;
    21.     }
    22.  
    23.     void Update () {
    24.         if(Input.GetMouseButtonDown(0)){
    25.             if(touchingPlatform){
    26.                 rigidbody.AddForce(jumpVelocity, ForceMode.VelocityChange);
    27.                 touchingPlatform = false;
    28.             }
    29.             else if(boosts > 0){
    30.                 rigidbody.AddForce(boostVelocity, ForceMode.VelocityChange);
    31.                 boosts -= 1;
    32.                 GUIManager.SetBoosts(boosts);
    33.             }
    34.         }
    35.         distanceTraveled = transform.localPosition.x;
    36.         GUIManager.SetDistance(distanceTraveled);
    37.      
    38.         if(transform.localPosition.y < gameOverY){
    39.             GameEventManager.TriggerGameOver();
    40.         }
    41.     }
    42.     void FixedUpdate () {
    43.         if(touchingPlatform){
    44.             rigidbody.AddForce(acceleration, 0f, 0f, ForceMode.Acceleration);
    45.         }
    46.     }
    47.     void OnCollisionEnter () {
    48.         touchingPlatform = true;
    49.     }
    50.     void OnCollisionExit () {
    51.         touchingPlatform = false;
    52.     }
    53.     private void GameStart () {
    54.         boosts = 0;
    55.         GUIManager.SetBoosts(boosts);
    56.         distanceTraveled = 0f;
    57.         GUIManager.SetDistance(distanceTraveled);
    58.         transform.localPosition = startPosition;
    59.         renderer.enabled = true;
    60.         rigidbody.isKinematic = false;
    61.         enabled = true;
    62.         longestDistanceTraveled = PlayerPrefs.GetFloat("longestDistance",0);
    63.     }
    64.  
    65.     private void GameOver () {
    66.         renderer.enabled = false;
    67.         rigidbody.isKinematic = true;
    68.         enabled = false;
    69.  
    70.         if(distanceTraveled > longestDistanceTraveled ) {
    71.                 longestDistanceTraveled = distanceTraveled;
    72.                 PlayerPrefs.SetFloat("longestDistance", longestDistanceTraveled);
    73.         }
    74.     }
    75.  
    76.     public static void AddBoost(){
    77.         boosts += 1;
    78.         GUIManager.SetBoosts(boosts);
    79.     }
    80. }
     
  6. Foieu

    Foieu

    Joined:
    Jan 31, 2015
    Posts:
    6
    Thanks you very much, I will try this, but is there a way of displaying my score?
     
  7. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    No, I only added code that loads, checks and saves the longestDistanceTraveled, making it persistent between games. You need to add a GUI Text to display it.
     
  8. Foieu

    Foieu

    Joined:
    Jan 31, 2015
    Posts:
    6
    Hello, Thank you very much for your replies, I have looked at the updated code and thanks to you now have a basic understanding of PlayerPrefs. But, I am still quite lost at figuring out how to display my score. I have created the code to display GUI items, with are just simple texts in the scene applied as variables to the GUImanager script. I understand that I need to create a new Public Static Void, which I have attempted on the last few lines of code, but how would I go about displaying the longestDistanceTraveled from the Runner.cs script, I am quite lost. Thanks for your help!

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class GUIManager : MonoBehaviour {
    4.    
    5.     private static GUIManager instance;
    6.    
    7.     public GUIText boostsText, distanceText, gameOverText, instructionsText, runnerText, HighscoreText;
    8.    
    9.     void Start () {
    10.         instance = this;
    11.         GameEventManager.GameStart += GameStart;
    12.         GameEventManager.GameOver += GameOver;
    13.         gameOverText.enabled = false;
    14.         HighscoreText.enabled = false;
    15.     }
    16.  
    17.     void Update () {
    18.         if(Input.GetMouseButtonDown(0)){
    19.             GameEventManager.TriggerGameStart();
    20.         }
    21.     }
    22.    
    23.     private void GameStart () {
    24.         gameOverText.enabled = false;
    25.         instructionsText.enabled = false;
    26.         runnerText.enabled = false;
    27.         enabled = false;
    28.         HighscoreText.enabled = true;
    29.     }
    30.    
    31.     private void GameOver () {
    32.         gameOverText.enabled = true;
    33.         instructionsText.enabled = true;
    34.         enabled = true;
    35.         HighscoreText.enabled = true;
    36.     }
    37.    
    38.     public static void SetBoosts(int boosts){
    39.         instance.boostsText.text = boosts.ToString();
    40.     }
    41.  
    42.     public static void SetDistance(float distance){
    43.         instance.distanceText.text = distance.ToString("f0");
    44.     }
    45.  
    46.     public static void SetHighscore(float LongestDistanceTraveled){
    47.         instance.HighscoreText.text = LongestDistanceTraveled.ToString("0");
    48.     }
    49. }
     
  9. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    This ought to do the trick:

    Code (CSharp):
    1. using UnityEngine;
    2. public class Runner : MonoBehaviour {
    3.     public static float distanceTraveled;
    4.     public static float longestDistanceTraveled;
    5.     private static int boosts;
    6.     public float acceleration;
    7.     public Vector3 boostVelocity, jumpVelocity;
    8.     public float gameOverY;
    9.     private bool touchingPlatform;
    10.     private Vector3 startPosition;
    11.     void Start () {
    12.         GameEventManager.GameStart += GameStart;
    13.         GameEventManager.GameOver += GameOver;
    14.         startPosition = transform.localPosition;
    15.         renderer.enabled = false;
    16.         rigidbody.isKinematic = true;
    17.         enabled = false;
    18.     }
    19.     void Update () {
    20.         if(Input.GetMouseButtonDown(0)){
    21.             if(touchingPlatform){
    22.                 rigidbody.AddForce(jumpVelocity, ForceMode.VelocityChange);
    23.                 touchingPlatform = false;
    24.             }
    25.             else if(boosts > 0){
    26.                 rigidbody.AddForce(boostVelocity, ForceMode.VelocityChange);
    27.                 boosts -= 1;
    28.                 GUIManager.SetBoosts(boosts);
    29.             }
    30.         }
    31.         distanceTraveled = transform.localPosition.x;
    32.         GUIManager.SetDistance(distanceTraveled);
    33.    
    34.         if(transform.localPosition.y < gameOverY){
    35.             GameEventManager.TriggerGameOver();
    36.         }
    37.     }
    38.     void FixedUpdate () {
    39.         if(touchingPlatform){
    40.             rigidbody.AddForce(acceleration, 0f, 0f, ForceMode.Acceleration);
    41.         }
    42.     }
    43.     void OnCollisionEnter () {
    44.         touchingPlatform = true;
    45.     }
    46.     void OnCollisionExit () {
    47.         touchingPlatform = false;
    48.     }
    49.     private void GameStart () {
    50.         boosts = 0;
    51.         GUIManager.SetBoosts(boosts);
    52.         distanceTraveled = 0f;
    53.         GUIManager.SetDistance(distanceTraveled);
    54.         transform.localPosition = startPosition;
    55.         renderer.enabled = true;
    56.         rigidbody.isKinematic = false;
    57.         enabled = true;
    58.         longestDistanceTraveled = PlayerPrefs.GetFloat("longestDistance",0);
    59.         GUIManager.SetHighscore(longestDistanceTraveled);
    60.     }
    61.     private void GameOver () {
    62.         renderer.enabled = false;
    63.         rigidbody.isKinematic = true;
    64.         enabled = false;
    65.         if(distanceTraveled > longestDistanceTraveled ) {
    66.                 longestDistanceTraveled = distanceTraveled;
    67.                 PlayerPrefs.SetFloat("longestDistance", longestDistanceTraveled);
    68.                 GUIManager.SetHighscore(longestDistanceTraveled);
    69.         }
    70.     }
    71.     public static void AddBoost(){
    72.         boosts += 1;
    73.         GUIManager.SetBoosts(boosts);
    74.     }
    75. }
     
  10. Foieu

    Foieu

    Joined:
    Jan 31, 2015
    Posts:
    6
    Thank you very much, it worked amazingly! I have learnt so much from you and really appreciate it! Just one question; if I have one highscore set on my editor, will that highscore start on another device when they first play the game, or will it start at 0?
     
  11. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897