- Home /
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 http://answers.unity3d.com/questions/218114/simple-saveload-scripting-for-multi-level-ios-game.html 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 :)
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;
Answer by whydoidoit · May 29, 2012 at 05:00 PM
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
So if saved the score, level reached and lives left like this:
PlayerPrefs.SetInt("SavedLevel", Application.loadedLevel); PlayerPrefs.Setint("Score"); PlayerPrefs.Setint("Lives");
would that work? or do I need to set an variable for say wall 1, wall 2, roof, floor, pad, for everything in the scene?
would I be correct in thinking in the ("") I would have to put the name of my score counting script and lives counting script?
sorry for all the questions! it's very hard to being new to javascript and scripting
You're ok, don't worry - we were all noobs once.
Yes I think you are fine saving just those things, just so long as you don't expect it to load with half a level complete or something. $$anonymous$$g. where things have moved from the starting point of that level. If things are different to how it would be at load level and you want to get back to exactly that state then you need to save information so you know what to change to make it the same as when it was saved.
It's:
PlayerPrefs.SetInt("Score", whateverYourScoreVariableIsCalled);
to save your score and
whateverYourScoreVariableIsCalled = PlayerPrefs.GetInt("Score");
etcetera
ok so for example to save my score, I have a GUItext called "scoretext" with a script called "Scorecounter".
So would I do this like:
PlayerPrefs.SetInt("Score"), GameObject.scoretext; ??
Your score must be in a variable right? Not just the Guitext or how would you add on to it?
It's
PlayerPrefs.SetInt("Score", score);
You won't get it off GameObject directly and your bracket is in the wrong place. $$anonymous$$aybe post some more code by editing your questoin?
Hi $$anonymous$$ike, I have updated my question (scroll down a bit) to include what I now have and info on my lives and score.
Answer by aldonaletto · May 24, 2012 at 12:15 AM
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.
@Aldo, thanks for the response. Sounds alot more complicated than I thought! The game I am making is a breakout/araknoid type game, I think the only things I would need to save are the players current level reached, lives and current score.
Would I do this by
PlayerPrefs.Setint("Score"); PlayerPrefs.Setint("Lives");
etc... would I be correct in thinking in the ("") I would have to put the name of my score counting script and lives counting script?
Or do I have to set a value for everything that is in my scene?
sorry if I sound really dumb
Your answer
Follow this Question
Related Questions
Serialization scripting error 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How can I save and load a player's position? 5 Answers
Load and Save Scenes with PlayerPrefs.Set/GetInt 0 Answers
Not loading an object if it have be retained from a previous scene. 1 Answer