highscore/display system for multiple levels

hey guys,
i have a scene set up with guiTexts: Level 1: , Level 2:, … i have a working save/load system using serialization but i don’t exactly know how to save the scores per level and displaying them in the guiTexts which are in a different scene. i also don’t know how to test if what i have so far is currently working right.

here is the saving script that saves at the end of the level and gets the score from an other script and saves it to highscores arraylist which then is saved to Highscores arraylist in the GameControl script:

using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public class Saving : MonoBehaviour
{
	ArrayList highscores = new ArrayList();
	ScoreManager Score;
	void Start(){
		 Score = GameObject.Find("Score").GetComponent<ScoreManager>();
	}


	void OnTriggerEnter( Collider other)
	{
		if(other.gameObject.tag == "Player")
		{
			GameObject[] NoOrbs;
			NoOrbs = GameObject.FindGameObjectsWithTag("Pickup");
			int count = NoOrbs.Length;
			if(count == 0){
			GameControl.control.levelcount += 1; //add +1 to levelcount for continue/levelLoading

				//here is the part of the code that saving the scores at the right position, i hope..
				int newScore = (int)Score.score; //get score and put it in int in newScore
				int rawlevelCount = Application.levelCount;
				int dontCountMenus = 2;
				int levelCount = rawlevelCount - dontCountMenus;
				highscores.Insert(levelCount, newScore);
				GameControl.control.Highscores = highscores; //save current highscores to GameControl Highscores
				GameControl.control.Save(); //saveGame

			}

and the GameControl script that should serializes the Array:

using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public class GameControl : MonoBehaviour {
	public static GameControl control;

	public int levelcount;
	public ArrayList Highscores = new ArrayList();
	void Awake () {
		if(control == null)
		{
			DontDestroyOnLoad(gameObject);
			control = this;
		}
		else if(control != this)
		{
			Destroy(gameObject);
		}
	}
	public void Save()
	{
		BinaryFormatter bf = new BinaryFormatter();
		FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
		
		PlayerData data = new PlayerData();
		data.levelcount = levelcount;
		data.Highscores = Highscores;

		bf.Serialize(file, data);
		file.Close();
	}
	public void Load()
	{
		if(File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
		{
			BinaryFormatter bf = new BinaryFormatter();
			FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
			PlayerData data = (PlayerData)bf.Deserialize(file);
			file.Close();

			levelcount = data.levelcount;
			Highscores = data.Highscores;
		}
	}
	public void overwriteSaveFile()
	{
		if(File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
		{
			BinaryFormatter bf = new BinaryFormatter();
			File.Delete(Application.persistentDataPath + "/playerInfo.dat");
		}
	}
}
[Serializable]
class PlayerData
{
	public int levelcount;
	public ArrayList Highscores;
}

none of these scripts give errors.

now what i need to find out is: is this currently working and if it does how do i write the variables from the array to the correct
guiText(called Level1score) “Level 1:” + score from array position 1;
guiText(called Level2score) “Level 2:” + score from array position 2;
etc until atleast 55 and leaving the ability to add another level( hence why i’m using arraylist instead of normal array)

thanks in advance

try to save the score to a .txt file and extract in the OnGui function when you need to access it in the start of the script.nthe txt file won’t change through loading levels and you have a lot more memory allowed to be saved to this file type.

write to a text file in resrouce folder - Unity Answers Check this out to save variables in a text file.

Use PlayerPrefs to save the highscores. Create a key for each level (e.g. PlayerPrefs.SetInt(“HighscoreLevel1”);). To display it, you can either read it directly from the player preferences, or save it to a local variable and read it from there.

Similar question: