- Home /
Save Values On Closing of Game
I have a simple scene set up that the player walks around and collects coins. Im using player prefs setInt to store the value of CoinsCollected and then player prefs getInt to get that saved value so it can be displayed on a different scene (The win screen). The game is an android build which is put onto a android tablet for testing purposes. I want the saved value to be saved so if the user quits the game and then later returns to it, the value will b available to be shown when the game loads. How would you go about getting the saved value to stay saved on closing of the game so it can be shown and changed when reopened ? Any help would be great, thanks.
Answer by HarshadK · May 20, 2015 at 02:15 PM
Go through this tutorial video: Persistence - Saving and Loading Data
I have looked through tutorial and tried to implement the Save and Load that he uses. I however need the game to auto load the saved info when the game starts up and to be displayed in a different scene from the title scene which loads first. The data to be saved needs to be saved when the player dies so that shouldnt be a problem by adding it to the code before loading the scene after the player dies. The problem im having is the variables such as Coins, Score are in scripts within the game...I need to access them variables in different scenes from different scripts so that the Save function can be as mentioned, when player dies in main scene, and Load function to be used as game loads up ?
You can write a data manager script with a singleton that will persist through the scenes. The method explained in that video with DontDestroyOnLoad.
This data manager can keep track of coins, score etc. You can load the values in the Start() method of this data namanger. Also whenever these values change you can set these required values in the data manager. And you can have a save method in your data manager to save the data that you can call when the player dies or for efficiency when the data manager gets destroyed.
Try watching the video a few more times and go through stack overflow on data persistence. I had the same problem in the beginning and believe me that video has all your answers you just need to use it in application with more understanding for your requirements
Thanks guys, great help! Got it working so that it saves the Coins Collected value and the loads in in another scene. Ill attach the two scripts below...What i want to do now is so the ObjectDestroy class will save its value as Save_Coins_Collected but when it saves the value, I want to add this to the previously saved value which would be loaded in the Upgrades_Script as TotalCoinsCollected. This will then be so the player collects coins and if they play again or close game and then play later, it will add the coins to there previous amount of coins. The coins will build up and allow them to buy things in game.
ObjectDestroy.cs public class ObjectDestroy : $$anonymous$$onoBehaviour {
void OnTriggerEnter(Collider other) //When a named trigger is entered...do something
{
if (other.tag == "ObjectDestroy")
{
Debug.Log ("HITTTTTTTTT"); //display in console if object hits the ObjectDestroy tag
Destroy (gameObject); // destroy object this script is attached to
}
if (other.tag == "Player")
{
Lives_2.Lives_Left --;
Time.timeScale = 0; // stop the game if player tag is hit
CoinsCollected.Coins_Collected = PlayerPrefs.GetInt ("Coins");
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create (Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData();
data.Save_Coins_Collected = CoinsCollected.Coins_Collected;
bf.Serialize (file, data);
file.Close();
Application.LoadLevel ("Lose_Screen"); //Load Lose_Screen scene
}
}
}
[Serializable]
class PlayerData
{
public int Save_Coins_Collected;
}
Upgrades_Script.cs public class Upgrades_Script : $$anonymous$$onoBehaviour {
public static int TotalCoinsCollected;
Text text;
void Awake()
{
text = GetComponent<Text> ();
}
// Use this for initialization
void Start ()
{
if (File.Exists (Application.persistentDataPath + "/playerInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", File$$anonymous$$ode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
TotalCoinsCollected = data.Save_Coins_Collected;
}
text.text = "Total Coins Collected: " + TotalCoinsCollected;
}
}
Your answer
Follow this Question
Related Questions
Adding Value to Already Saved Values 1 Answer
How to save players rotation in Unity? PlayerPrefs 3 Answers
Save/Load Animation State of Instantiated Prefabs 0 Answers
PlayerPrefs not saving 1 Answer