Search Unity

Data serialization method - Saving High Score Per Level & displaying on UI Text in Menu

Discussion in 'Immediate Mode GUI (IMGUI)' started by iMancha, Oct 10, 2014.

  1. iMancha

    iMancha

    Joined:
    Sep 22, 2014
    Posts:
    25
    Hi All

    I need to update the HighScore Text with the new score for each level(scene) and make sure it remains there until the player gets a higher score, I am not sure of the correct method in accessing these UI texts (they are child of a UI button) from the scripts.

    Attached UI image shows the buttons with their child components.
    I need the text - "Highscore : "of HighScoreTextLvl01-14 to update when a highscore is made.


    Here my build settings:




    The console error received when animation is triggered for game over or win state:

    ArgumentOutOfRangeException: Index must be >= 0 and <= Count.

    Parameter name: index

    15

    System.Collections.ArrayList.ThrowNewArgumentOutOfRangeException (System.String name, System.Object actual, System.String message) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections/ArrayList.cs:3261)

    System.Collections.ArrayList.Insert (Int32 index, System.Object value) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections/ArrayList.cs:2940)

    GameOverManager.Update () (at Assets/Resources/Scripts/Managers/GameOverManager.cs:36)


    Here is the GameOverManager:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System;
    6. using System.Runtime.Serialization.Formatters.Binary;
    7. using System.IO;
    8.  
    9. public class GameOverManager : MonoBehaviour
    10. {
    11.  
    12.     public Slider energySlider;
    13.     public Slider countDownSlider;
    14.     public Slider lifeSlider;
    15.  
    16.     ArrayList highScores = new ArrayList();
    17.     Animator anim;
    18.  
    19.  
    20.  
    21.     void Awake()
    22.     {
    23.         anim = GetComponent<Animator>();
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.      
    29.         if(energySlider.value >= 300)
    30.         {
    31.             anim.SetTrigger("NextLevel");  
    32.             int newScore = (int)ScoreManager.score; //get score and put it in int in newScore
    33.             int rawlevelCount = Application.levelCount;
    34.             int dontCountMenus = 2;
    35.             int levelCount = rawlevelCount - dontCountMenus;
    36.             highScores.Insert(levelCount, newScore);
    37.             GameControl.control.highScores = highScores; //save current highscores to GameControl Highscores
    38.             GameControl.control.Save(); //saveGame
    39.         }
    40.      
    41.         if (countDownSlider.value >=300)
    42.         {
    43.             anim.SetTrigger("GameOver");
    44.             int newScore = (int)ScoreManager.score; //get score and put it in int in newScore
    45.             int rawlevelCount = Application.levelCount;
    46.             int dontCountMenus = 2;
    47.             int levelCount = rawlevelCount - dontCountMenus;
    48.             highScores.Insert(levelCount, newScore);
    49.             GameControl.control.highScores = highScores; //save current highscores to GameControl Highscores
    50.             GameControl.control.Save(); //saveGame
    51.         }
    52.  
    53.         if (lifeSlider.value <=0)
    54.         {
    55.                 anim.SetTrigger("GameOver");
    56.             int newScore = (int)ScoreManager.score; //get score and put it in int in newScore
    57.             int rawlevelCount = Application.levelCount;
    58.             int dontCountMenus = 2;
    59.             int levelCount = rawlevelCount - dontCountMenus;
    60.             highScores.Insert(levelCount, newScore);
    61.             GameControl.control.highScores = highScores; //save current highscores to GameControl Highscores
    62.             GameControl.control.Save(); //saveGame
    63.         }
    64.     }
    65.  
    66. }
    67.  
    and here is my GameControl:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System;
    5. using System.Runtime.Serialization.Formatters.Binary;
    6. using System.IO;
    7.  
    8. public class GameControl : MonoBehaviour {
    9.     public static GameControl control;
    10.  
    11.     public int levelCount;
    12.     public ArrayList highScores = new ArrayList();
    13.  
    14.  
    15.     public void Save()
    16.     {
    17.         BinaryFormatter bf = new BinaryFormatter();
    18.         FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
    19.      
    20.         PlayerData data = new PlayerData();
    21.         data.levelCount = levelCount;
    22.         data.highScores = highScores;
    23.  
    24.         bf.Serialize(file, data);
    25.         file.Close();
    26.     }
    27.     public void Load()
    28.     {
    29.         if(File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
    30.         {
    31.             BinaryFormatter bf = new BinaryFormatter();
    32.             FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
    33.             PlayerData data = (PlayerData)bf.Deserialize(file);
    34.             file.Close();
    35.          
    36.             levelCount = data.levelCount;
    37.             highScores = data.highScores;
    38.         }
    39.     }
    40.  
    41.     [Serializable]
    42.     class PlayerData
    43.     {
    44.         public int levelCount;
    45.         public ArrayList highScores;
    46.     }
    47. }
    Finally here is my ScoreManager:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class ScoreManager : MonoBehaviour
    7. {
    8.     public static int score;
    9.     public static int energy;
    10.  
    11.     Text text;
    12.  
    13.     void Awake ()
    14.     {
    15.         text = GetComponent <Text> ();
    16.         score = 0;
    17.     }
    18.  
    19.     void Update ()
    20.     {
    21.         text.text = "Score: " + score;
    22.     }
    23. }
    If anyone can help, it would be great. There are no tutorials showing how the data serialization works with the NEW UI, I watched this live training video 20.Live_Training_3_Mar_2014_-_Data_Persistence and it uses old GUI interface.

    I don't want to use playerprefs as it isn't very secure.

    Thanks
     

    Attached Files:

  2. Thewhiteaura

    Thewhiteaura

    Joined:
    Mar 19, 2015
    Posts:
    7
    levelCount seems to be original unity API, did you intend to call that? if your setting it as a variable maybe try tweaking the name slightly, maybe its clashing with the internals. I've had that before and it caused a lot of problems, I see this is an older thread and would hope that you have solved this problem already but I noticed that by clicking on levelCount.