- Home /
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
Answer by SnotE101 · Sep 19, 2014 at 07:18 PM
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.
http://answers.unity3d.com/questions/54822/write-to-a-text-file-in-resrouce-folder.html Check this out to save variables in a text file.
won't players be able to go in the textfile and edit the scores this way? and its already being saved in binary format by the gamecontrol script or am i misunderstanding your answer?
i have updated my question, could you take another look at it? thank you
Answer by pgomes · Sep 19, 2014 at 10:32 PM
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:
http://answers.unity3d.com/questions/596490/how-to-save-high-scores.html
thank you for taking the time to answer, i'm a beginner so i'm sorry if i got this wrong but i've been reading on google that i should avoid playerprefs because they store in the register and that the contents of playerprefs can be easily changed by the player and if i get my game to a multiplayer level i would set up rankings for the best players and i wouldn't want people to change their score to get higher into the ranking. either way i will look at the link you provided but now that i have mentioned this do you still think i should go for playerprefs ins$$anonymous$$d of finding a way to do it with my current save/load system thats using serialization and formatting it to binary?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Runs fine in editor, crash on switch platform to iOS. 0 Answers
How to Move an object after it was reproduced by spawn 0 Answers
Mathf.pingpong 4 Answers