Help with my save and load script please

I am trying to figure out how to do a simple save and load script so the player can press save (during any level via a 3d text with colision on each level) and then if they die or quit they can reload the level they reached by pressing load from the menu screen.

I saw this question Simple save/load scripting for multi level iOS game. - Questions & Answers - Unity Discussions and used the info from answer number 1 to see if I could make my own (see below).

MY SAVE SCRIPT:

#pragma strict
//put this script on save button
var mySound : AudioClip;
var mySound1: AudioClip;


function OnMouseEnter()
{
	//change color of the text
	renderer.material.color = Color.red;
	audio.clip = mySound;
	audio.Play();
}

function OnMouseExit()
{
	//change the color of the text
	renderer.material.color = Color.white;
}

function OnMouseUp() { 

              //Save level
              audio.clip = mySound1; 
              audio.Play();
			  PlayerPrefs.SetInt("SavedLevel", Application.loadedLevel); 
}

=========

I also have made this load/continue script (put on a 3d text with collider), (this is on my main menu screen) below:

MY LOAD/CONTINUE SCRIPT

#pragma strict
//put this script on load/continue button
var mySound : AudioClip;
var mySound1: AudioClip;


function OnMouseEnter()
{
	//change color of the text
	renderer.material.color = Color.red;
	audio.clip = mySound;
	audio.Play();
}

function OnMouseExit()
{
	//change the color of the text
	renderer.material.color = Color.white;
}

function OnMouseUp() { 

              audio.clip = mySound1; 
              audio.Play();
              
              //Load level
			  if (PlayerPrefs.HasKey("SavedLevel"))
  			  Application.LoadLevel(PlayerPrefs.GetInt("SavedLevel"));
}

========================

To test this out, I put a save button on level 2 of my game and played it for a few seconds then pressed my save button. I then purposefully lost all my lives and from my game over screen went back to my main menu and pressed my load button.

When I pressed it it loaded level 2 (cool!) but it quickly went to my game over screen as if it had saved at the point I lost all my lives and NOT at the point that I pressed save (after a few seconds of play on level 2).

Is there something I have missed out or doing wrong? please advise me if you can :slight_smile:

PS this is in javascript and it’s for Webplayer/PC


UPDATED 29/05/2012


@Mike (whydoidoit)

NOW ON MY SAVE SCRIPT:

function OnMouseUp() {

          //Save level
          audio.clip = mySound1; 
          audio.Play();
		  PlayerPrefs.SetInt("SavedLevel", Application.loadedLevel);
		  PlayerPrefs.SetInt("Score", Scorecounter);
		  PlayerPrefs.SetInt("Lives", LIVES);

}

=====================

NOW ON MY LOAD SCRIPT

function OnMouseUp() {

          audio.clip = mySound1; 
          audio.Play();
          
          //Load Saved score, lives and then load saved level
		  PlayerPrefs.GetInt("SavedLevel", Application.loadedLevel);
		  PlayerPrefs.GetInt("Score", Scorecounter);
		  PlayerPrefs.GetInt("Lives", LIVES);
		  Application.LoadLevel(PlayerPrefs.GetInt("SavedLevel"));

}

===================

THIS IS MY SCORE TRIGGER SCRIPT

function OnTriggerEnter(enterer : Collider)
{
if (enterer.collider.gameObject.CompareTag(“Bullet”))
{
Debug.Log(“Score!”);
Scorecounter.Counter ++;
}
}

=======================

THIS IS MY SCORE COUNTER SCRIPT

static var Counter : int = 0;

function Update ()
{
guiText.text = "Score: " + Counter;
}

MY LIVES SCRIPT

var lives : Texture2D; //one life left
var lives2 : Texture2D; //one life left
var lives3 : Texture2D; //one life left
var lives4 : Texture2D; //one life left
var lives5 : Texture2D; //one life left
var lives6 : Texture2D; //one life left
var lives7 : Texture2D; //one life left
var lives8 : Texture2D; //one life left
var lives9 : Texture2D; //one life left

static var LIVES = 5;

function Update ()
{
switch(LIVES)
{

	case 9:
		guiTexture.texture = lives9;
	break;
		
	case 8:
		guiTexture.texture = lives8;
	break;
			
	case 7:
		guiTexture.texture = lives7;
	break;
		
	case 6:
		guiTexture.texture = lives6;
	break;
		
	case 5:
		guiTexture.texture = lives5;
	break;
		
	case 4:
		guiTexture.texture = lives4;
	break;
		
	case 3:
		guiTexture.texture = lives3;
	break;
	
	case 2:
		guiTexture.texture = lives2;
	break;
	
	case 1:
		guiTexture.texture = lives;
	break;
	
	case 0:
		Application.LoadLevel(54);
}	

}

THE PART IN MY FLOOR COLISION SCRIPT THAT REMOVES A LIFE WHEN BALL HITS THE FLOOR

lives.LIVES -= 1;

I’m sorry to inform you that the only thing saved in your code is the level index - not even a single bit of other level data was saved.

Saving and loading level information is a much more complex subject: you must first define what exactly must be saved - each item will require a PlayerPrefs.SetInt, SetFloat or SetString to be saved.

But saving and retrieving player data (position, health, score, lives, weapons etc.) is easy; handling other scene objects may be much more complicated: collectable items like weapons, ammunition, health packs etc. must be managed - when loading a saved level, already collected items must be deleted or not instantiated. Enemies are even harder: at least a dead/alive status for each enemy must be saved, so dead enemies will not respawn from hell when the saved level is loaded - and in more complex cases you may also have to save/retrieve enemy’s position, rotation, health, AI status etc.

So you have a number of choices:

Choice one:

  • You save every variable you want using PlayerPrefs.SetInt(“NameOfSavedVariable”, savedVariable); or string or whatever
  • You read your values back in Start() using savedVariable = PlayerPrefs.GetInt(“NameOfSavedVariable”)

Choice two:

  • You use XmlSerializer. This will serialize a class into an XML string that you can save in PlayerPrefs using PlayerPrefs.SetString and then you can deserialize it when you start up.
  • You probably want to make a helper class to hold all of your game state as serializing a MonoBehaviour isn’t going to do it for you.
  • Mark the class with a [Serializable] attribute.

Choice three:

  • Use something like EZ Game Saver from the asset store that will help you out with a bunch of things