- Home /
Multiple Save (Saving & Loading System) With PlayerPrefs
Ok I have a single save, saving/loading system using player prefs and it works, but whenever I have a multiple saving/loading system, no matter what I try it won't work under any circumstances. It will save the values but whenever I try to play the game there is something wrong, the values get shared, switched, or it doesn't work at all. I've been trying to fix this for three days.
This is how the system is supposed to work. In the main menu you can press a button that will set the curGame1 or curGame2 int to 1 (0 means that this is not the active game, 1 means it is), it will also load the first level. In the first level there are two game objects with the saving/loading system code attached to a game object, when the scene loads one of the will delete themselves while the other is left to set the player's values (this was a last resort after EVERY other method failed). In the pause menu, when you leave the level to go back to the menu it sets the playerpref int "AlreadyPlayed[num]" to 1 if you played in that curGame. Once this is done the next time you load the scene, the variables will be set for you, therefore loading the variables from the last time you played. It's not that complex, but I do need to start commenting my code.
Please do not recommend something on the asset store to me, I'm learning how to do things myself, not how to buy stuff (I already know how to do that). Plus I'm only saving a few variables for now, not the entire state of the scene.
Here's the code I'm using (there are several):
The in-game saving/loading system
var money : int;
var player : GameObject;
var reset : boolean;
var playerVars : Roll;
var ammo : Gun;
var gun : GameObject;
function Start () {
if(PlayerPrefs.GetInt("CurGame1", 0) && PlayerPrefs.GetInt("CurGame2", 1)){
Destroy (gameObject);
}
player = GameObject.Find ("player");
gun = GameObject.Find ("gun");
playerVars = player.GetComponent(Roll);
ammo = discShoot.GetComponent(Gun);
//Player prefs for money
//Already played controls the alteration of health, shield, ect.
if(PlayerPrefs.GetInt("alreadyPlayed1", 1)){
money = PlayerPrefs.GetInt("Money1");
playerVars.curHealth = PlayerPrefs.GetInt("Health1");
playerVars.curShield = PlayerPrefs.GetInt("Shield1");
ammo.ammo = PlayerPrefs.GetInt("Ammo1");
}else
PlayerPrefs.SetInt("Health1", 100);
PlayerPrefs.SetInt("Shield1", 100);
PlayerPrefs.SetInt("Ammo1", 100);
}
function Update () {
//Getting vars in them
//Setting the ints
PlayerPrefs.SetInt("Money1", money);
PlayerPrefs.SetInt("Health1", playerVars.curHealth);
PlayerPrefs.SetInt("Shield1", playerVars.curShield);
PlayerPrefs.SetInt("Ammo1", ammo.ammo);
//Temporary reset function (only works after you stop and start the game again)
if(reset){
PlayerPrefs.SetInt("alreadyPlayed1", 0);
PlayerPrefs.SetInt("Money1", 0);
money = 0;
reset = false;
}
}
The loading menu code (the part that matters)
if(GUILayout.Button(buttonString +1)){
PlayerPrefs.SetInt("CurGame1", 1);
PlayerPrefs.SetInt("CurGame2", 0);
Application.LoadLevel("Ocean");
}
GUILayout.Space(spacing);
if(GUILayout.Button(buttonString +2)){
PlayerPrefs.SetInt("CurGame1", 0);
PlayerPrefs.SetInt("CurGame2", 1);
Application.LoadLevel("Ocean");
}
Pause menu code (the part that matters)
if(PlayerPrefs.GetInt("CurGame1", 1) && PlayerPrefs.GetInt("CurGame2", 0)){
PlayerPrefs.SetInt("AlreadyPlayed1", 1);
}else
if(PlayerPrefs.GetInt("CurGame1", 0) && PlayerPrefs.GetInt("CurGame2", 1)){
PlayerPrefs.SetInt("AlreadyPlayed2", 1);
}
Answer by Chimera3D · Jun 08, 2012 at 03:26 AM
I fail, I wasn't using the code correctly. I fixed it. I didn't realize I had to get the int of the PlayerPrefs then compare that to the 1 or 0.
Answer by rmele09 · Aug 27, 2013 at 05:39 PM
could you repost the final code so we can see what you fixed?
Your answer
Follow this Question
Related Questions
Saving with PlayerPrefs? How it works? 1 Answer
Where does unity save player preds when testing in the editor? 2 Answers
Where is PlayerPrefs Located on Windows? 2 Answers
PlayerPrefs - Sound slider values 1 Answer
How to consume input when automatically switching control schemes through a Player Input Component 0 Answers