Help with saving and loading a game please!

I am a complete newbie when it comes to saving and loading a file with Playerprefs. I’m creating a simple puzzle game, where after each level, a GUI appears with a button to save the progress. If the App Quits or the user dies, I have a resume button on the Main Menu to load the last saved game. Here’s my two scripts:

End of Level Script:

function OnGUI () {
	
if (GUI.Button(Rect(65,90,200,50),"Proceed to Next Level")) {
	Application.LoadLevel(5);
	
}

if (GUI.Button(Rect(65,160,200,50), "Quit Game")) {
	Application.LoadLevel(1);
	
}

if (GUI.Button(Rect(65,230,200,50), "Save Game")) {
	
	PlayerPrefs.SetInt("Level", 1);
	
}

   }

Here’s the Main Menu script:

function OnGUI () {
	

	if (GUI.Button (Rect (65,90,200,50), "Play")) {
		Application.LoadLevel (2);
	}
	
	if (GUI.Button (Rect (65,160,200,50), "Resume")) {
	PlayerPrefs.GetInt("Level", 1);
	}
	
}

So far this is not working, and I know there’s more to it then thsi but I am completely lost! Please help!

PlayerPrefs setInt and getInt are only storing an integer (assumably the integer indicating which level they last were at.) After you get the point of the last save, you have to load the level using that value.

function OnGUI () {
   

   if (GUI.Button (Rect (65,90,200,50), "Play")) {
      Application.LoadLevel (2);
   }
   
   if (GUI.Button (Rect (65,160,200,50), "Resume")) {
      var savedLevel = PlayerPrefs.GetInt("Level", 1);
      Application.LoadLevel( savedLevel );
   }
   
}

Hmmm ok. I see that you declared a variable and connected it to GetInt. Is the SetInt code correct then? I tried it but it still doesn’t save. Do I have to declare the savedLevel variable in there too? Thanks alot for your help, I’m so lost!

brandon

In your Build Settings, notice that each scene has it’s own index. The index that you will be setting with setInt should match whatever level you are trying to load. If the menu is index 1 then when you attempt to load the level, you are just re-loading the menu.

For saving the level you want:

if (GUI.Button(Rect(65,230,200,50), "Save Game")) {
   
   PlayerPrefs.SetInt("Level", <NEXT_LEVEL'S_INDEX_HERE>);
   
}

So for your example of having completed level 4 (assuming you’re going in order) and the next stage being level 5; you’d want:

if (GUI.Button(Rect(65,230,200,50), "Save Game")) {
   
   PlayerPrefs.SetInt("Level", 5);
   
}

Ahh ok I see! Thanks alot man! I hate to be bother (again), but I have one more small issue. The way my scenes are being loaded is level 0 is actually a second splash screen instead of the traditional main menu. When the game is booted up for the first time, the Resume button is loading the first scene (index 0), because the savedLevel variable hasn’t been saved yet I’m assuming. Normally this wouldn’t be a problem, but when the game is launched for the first time, the Resume button is active and loads the Splash Screen when pressed. Is there a way to tell the Resume button to be inactive until the PlayerPrefs.SetInt has been called? Thanks again, I owe you one man!

nice post dude…

simulation de credit
rachat simulateur[/url] - vous n’avez à aller très
loin pour une simulation de rachat de crédit

if (PlayerPrefs.HasKey("Level"))
{
 // Load your level code here
}
else
{
 // Do nothing, or nag the player that this doesn't do anything
}

That should help you out :slight_smile:

Awesome, thanks alot guys! thats why I love these forums :slight_smile: