I've got a simple set up here. Each level is represented by a GameObject that hold information about the level. It also holds the Score that the player has achieved if they've played the level before but it doesn't seem to be working. How is this script?
Code: Select all
public GameObject[] levels; //An Array of all the levels.
private List<int> scores = new List<int>(); // A list of the scores from each level.
private List<int> loadedScores = new List<int>(); // A list of the scores once they're loaded. Shouldn't really be needed.
public void Start()
{
LoadGame(); // Loads the game when the game starts.
//This is here if the player has just finished a level and has come back to the level select scene. This is working.
if (scoreKeeper.levelFinished == true)
{
GameObject.Find(scoreKeeper.currentLevel.ToString()).GetComponent<levelManager>().currentScore = scoreKeeper.lastScore;
scoreKeeper.currentLevel = 0;
scoreKeeper.lastScore = 0;
scoreKeeper.levelFinished = false;
}
//This is then saved.
SaveGame();
}
public void SaveGame()
{
//Gets each level object from all the levels and adds the score to the list.
foreach(GameObject level in levels)
{
scores.Add(level.GetComponent<levelManager>().currentScore);
}
//Saves the new list.
ES3.Save("scoreSave", scores);
}
public void LoadGame()
{
//This loads the scores into a list
loadedScores = ES3.Load("scoreSave", loadedScores);
//This gets each level object from the list
foreach (GameObject level in levels)
{
//This gets each score from the loaded score list
foreach(int scoreNum in loadedScores)
{
//Adds the score to each level. I think this is where theres an issue.
level.GetComponent<levelManager>().currentScore = scoreNum;
}
}
}
private void OnApplicationQuit()
{
SaveGame();
}
Thanks again!