Hi! I'm trying to save my 'coins' and 'points' and 'waves' in my 2d game I'm working on.. I've tried dontdestroyonload but didn't work for what i needed so I've looked into saving as playerprefs but im new to all of this.. I had 50levels and 50 difference scenes but decided to just have all levels on 1 scene and just use timers to display 'Next Level' text etc but I need a way to save my coins, castle level, points, and upgrades purchased.. This is the script which i believe must be edited to allow saving.. I'm new to this so please dumb down answers , i just want my game to save and load how i left it when i exit, thank you! Code (CSharp): using UnityEngine; using UnityEngine.UI; public class ResourcesCastle : MonoBehaviour { public int Coins; public int Experience; public int LevelCastle = 1; public int Wave = 1; public float Timers = 120f; public GameObject Cl1; public GameObject Cl2; public GameObject Cl3; public Text CoinsText; public Text ExperienceText; public Text LevelCastleText; public Text WaveText; public Text Timer; private float times; private bool Wave2 = false; private bool Wave3 = false; private bool Wave4 = false; private bool Wave5 = false; void Start() { times = Timers; } void Update() { WaveUp(); Texts(); Waves(); LvlCastle(); } void WaveUp() { if(Wave5 == true) { Wave = 5; } if(Wave == 2 && Wave2 == false) { gameObject.GetComponent<EnemySpawn>().TimerMax -= 2; Wave2 = true; } if(Wave == 3 && Wave3 == false) { gameObject.GetComponent<EnemySpawn>().TimerMax -= 3; Wave3 = true; } if(Wave == 4 && Wave4 == false) { gameObject.GetComponent<EnemySpawn>().TimerMin -= 1; gameObject.GetComponent<EnemySpawn>().TimerMax -= 4; Wave4 = true; } if (Wave == 5 && Wave5 == false) { gameObject.GetComponent<EnemySpawn>().TimerMin -= 1; gameObject.GetComponent<EnemySpawn>().TimerMax -= 5; Wave5 = true; } } void Texts() { CoinsText.text = Coins.ToString(); ExperienceText.text = Experience.ToString(); LevelCastleText.text = LevelCastle.ToString(); WaveText.text = "Wave:" + Wave.ToString(); } void Waves() { times -= Time.deltaTime; if(times <= 0) { Wave += 1; times = Timers; } } void LvlCastle() { if (Cl1.activeInHierarchy == true) { LevelCastle = 1; } if (Cl2.activeInHierarchy == true) { LevelCastle = 2; } if (Cl3.activeInHierarchy == true) { LevelCastle = 3; } } }
PlayerPrefs are very easy to use and reading about them and seeing examples should be all you need: https://docs.unity3d.com/ScriptReference/PlayerPrefs.html You can do 5 things with PlayerPrefs: Store a value (int, float, string) Save PlayerPrefs current values to the local storage Check if PlayerPrefs contains a value previously stored Retrieve a value (int, float, string) Delete values from the storage Here's an example of each thing: Code (CSharp): private int coins; private string coins_key = "coins"; public void Save() { PlayerPrefs.SetInt(coins_key, coins); // 1 PlayerPrefs.Save(); // 2 } public void Load() { if (PlayerPrefs.HasKey(coins_key)) // 3 { coins = PlayerPrefs.GetInt(coins_key); // 4 } } public void Clear() { PlayerPrefs.DeleteKey(coins_key); // 5 PlayerPrefs.Save(); // 2 } Unity will automatically call "PlayerPrefs.Save" when the application quits normally, but if it crashes or something, you will lose data. So it's best to save at regular intervals in code.
Thank you for the reply! Ive taken a look at the playerprefs help section multiple times, im still new to coding and all that and I've no idea where to even put the code inside the script (im sorry). Also, like you said if it crashes unexpectedly then it wont save which would be a huge problem if i have a bug in my game or simply if the players phone dies, so I'd need to code a script and attach to a save button for example that will save via the scripts code etc? Sorry for the stupid questions but I've been working on this game for a while and I've just suddenly hit a brick wall haha!
Choose a time you want to save the game. It could be every time you change your coins and other values, it could be at the end of a level, or only when the player presses a button. Then at that time, call your "Save" function, which will store all the values into PlayerPrefs and save them. You can add to the function so that it saves coins as well as anything else you want. When the game starts up, use a Start or Awake function to call "Load", which will assign your "coins" variable and others with the stored values. That's pretty much it. Load on startup, save when things change.
Thanks again for the reply and trying to help! I hope someone sees this thread and also gets help, i have messaged you as im a little confused though :s