- Home /
Save data in Single-Player
Hi!
I need to save data such as inventory info, level and stuffs like these. I tried to save them into the Assets folder as txt file, but this doesn't work during runtime (I already read the premade files from the Assets folder, but i cannot Update them). I was thinking about regedit, but I would like to avoid using it.
How can I solve my problem?
Thanks for the reply, bye.
Answer by ForeignGod · Mar 13, 2015 at 07:53 AM
Lets say you played your game and got to lvl 10 You would want to save the variable Lvl 10 and load it some other time.
You can do this by doing PlayerPrefs
var Lvl = 10;
//How we are going to save your level
function SaveLevel ()
{
PlayerPrefs.SetInt("SavedLevel",Lvl);
//We now stored the Lvl variable and saved it as "SavedLevel"
}
//When you press the launch button you want to execute this function.
//Here we are getting the information we stored earlier and then inserting it into the Lvl variable
function LoadLevel ()
{
Lvl = PlayerPrefs.GetInt("SavedLevel");
}
I wrote this code in here and this is not tested, but this is just to show you how you can do it, if you want to go more in depth i suggest you read about PlayerPrefs here http://docs.unity3d.com/ScriptReference/PlayerPrefs.html
Again this is not for copy paste but merely so you understand the way to do it, atleast a very simple way.
As I see the PlayerPrefs saves into the registry which I would like to avoid as I wrote above, but If there is no other solution, I'll use it. Could you tell me some other ways, if there is any? Thanks! PS.: The reason why I wouldn't like to use it is because, I cannot move the game and the saves with it to an other computer without problem.
Well you could go the hard way, using UNITY>PHP>SQL. I tried this before but it was really hard. http://answers.unity3d.com/questions/698234/sending-values-from-unity-to-php-to-sql.html
This is a question i asked awhile ago, you could probably find some of it usefull but most of it was made for my own project.
$$anonymous$$aking a database using both UNITY to PHP to a SQL database will take dedication to make it work. There might be assets already in the store, but this is what i did atleast.
If you end up using PlayerPrefs anyway make sure to call PlayerPrefs.Save when quitting the game. ^^
Thank you! I will use PlayerPrefs and make the player remember: when you move to an other computer, the save will be lost (or maybe I can make a save-load algorithm so the user will be able to save and load their progress) Thanks a lot! Have a nice day!