Saving Options

Hello,

Im struggling with how to save my game and also how to load the next level? and need some insights and possibly code tips? Here’s whats happening:

From level 1 if the player dies it will reload level 1 and start back at the beginning. If level 1 is beat it will load level 2.
The problem Im having is with this is once Level2 is loaded my CoinDisplay (gui text) wont be updated or the Inventory wont have any items in it.

So I decided to add a script to these items (gui text, inventory, player) Called (SurviveLevelLoad):

function Awake () 
{
    DontDestroyOnLoad (transform.gameObject);
}

Which has worked great going from level1 to level2 but if the player dies in level1 and it has to be reloaded then it will create a bunch of objects like 3 gui text objets, 3 Inventorys and 3 or more players.

So now Im not sure what to do?

One way I dont have anything updated when getting to level2 and SurviveLevelLoad works great when going from level to another level but really messes up when you have to restart a level over.

Any help or insights would be great! (eyes are getting heavy).

There are 3 ways off of the top of my head you could go about this.

Option 1:
When loading a level, check whether there are duplicate objects. If there are, delete one.

Option 2:
When loading a level, check whether there is an object. If not, create one. In this scenario, you wouldn’t have any in your scene by default, it would be Instantiated by scripting.

Option 3:
Create a scene before loading a level which only has the objects you don’t want destroyed. Then go to your desired level, and don’t load this scene again. Example: MainMenu->IntermediateScene(has the don’t destroy objects)->Level1->Retry->Level1

The easiest option would be a scene independant class where you store your info and then access it from any level in the game.
Create a file called Saver.js and put something like this in it :
static var Score:int = 0;
Then in the first scene save your score :
Saver.Score = 1;
In any other scene you can get the score :
var i:int = Saver.Score;

Thanks for the tips it REALLY helps a LOT. Im going to try a few variations of your ideas :slight_smile:

I’ll write back and let you know what I come up with :smile:

Ok so what I did was leave the SurviveLevelLoad and I just put it in an update function with a boolean true or false. When the level was complete a button would pop-up and set it to true, if the health of the player was zero another button would pop-up and reload the level which I made a string that would keep loading level1. If the Level one was beat along with making survivelevelload I would also pass the string for the other pop-up button this way inside of level2 if you died it would keep loading that level and so forth for the rest of the levels which seemed to work out very well :slight_smile:

Thank you both very very much!