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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

to save high score

Discussion in 'Scripting' started by Linus392, Jul 11, 2015.

  1. Linus392

    Linus392

    Joined:
    Aug 14, 2013
    Posts:
    24
    how to save high score like that of flappy bird using c#
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
  3. Rad-Coders

    Rad-Coders

    Joined:
    Apr 10, 2015
    Posts:
    36
    Use something like this or just use streemwriter to write to files. I would personally avoid using .txt files.

    Code (CSharp):
    1. using System.Runtime.Serialization.Formatters.Binary;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine;
    5. using System.IO;
    6. using System;
    7. public class ScoreManager : MonoBehaviour
    8. {
    9.     public int score;
    10.      public int highscore;
    11.      Text scoreText;
    12.      void Start ()
    13.      {
    14.          scoreText = GetComponent<Text>();
    15.          score = 0;
    16.         Load();
    17.      }
    18.      void Update ()
    19.      {
    20.         if(score > highscore)
    21.          {
    22.              highscore = score;
    23.              scoreText.text = highscore.ToString();;
    24.          }
    25.      }
    26.      public void AddScore (int addedScorePoints)
    27.      {
    28.          score += addedScorePoints;
    29.      }
    30.      public void ResetScore ()
    31.      {
    32.          score = 0;
    33.      }
    34.      public void Save ()
    35.      {
    36.          if(Directory.Exists(Application.dataPath + "/Save data/") == false)
    37.              Directory.CreateDirectory(Application.dataPath + "/Save data/");
    38.          BinaryFormatter bf = new BinaryFormatter ();
    39.          FileStream file = File.Create (Application.dataPath + "/Save data/Score.secure");
    40.          ScoreData data = new ScoreData ();
    41.        
    42.          data.highscore = this.highscore;
    43.        
    44.          bf.Serialize (file, data);
    45.          file.Close ();
    46.      }
    47.  
    48.      public void Load ()
    49.      {
    50.          if(File.Exists(Application.dataPath + "/Save data/Score.secure"))
    51.          {
    52.             BinaryFormatter bf = new BinaryFormatter();
    53.              FileStream file = File.Open(Application.dataPath + "/Save data/Score.secure", FileMode.Open);
    54.              ScoreData data = (ScoreData)bf.Deserialize(file);
    55.              file.Close();
    56.            
    57.             this.highscore = data.highscore;
    58.              scoreText.text = highscore.ToString();
    59.          }
    60.      }
    61. }
    62.  
    63. [Serializable]
    64. class ScoreData
    65. {
    66.      public int highscore;
    67.  }
    68.  
     
  4. Linus392

    Linus392

    Joined:
    Aug 14, 2013
    Posts:
    24
    thanks ! i will try