- Home /
Save game progress at respawn points
Hi, I am new to unity and hoping someone can help. I have a 2d Platformer game. It is one large level, but as the player progresses through the level there are respawn points. when the player passes them, it saves the players position so when he dies he returns to this point. I have this working perfectly.
However, I also need the game to save the progress at this point - i.e. all enemies killed, all coins collected, all buttons pushed etc. If the player dies before he gets to the next respawn point: - He returns to the last respawn point he passed, - Everything he did after passing that respawn point is reset and has to be done again - Everything he did before passing that respawn point stays exactly as he left it (i.e. all coins collected etc).
There are literally hundreds of coins and objects. What would be the best way to get unity to remember the progress to that point?
Thanks, James
Answer by RadonRaph · Nov 17, 2019 at 02:21 PM
Hello @jameshutson108, There is few ways to save something in Unity, The easiest is to using player prefs !
https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
This work fine and its easy to use. You save something with << PlayerPrefs.SetInt("MyObjectName",0);
>> and get it with << int activeness = PlayerPrefs.GetInt("MyObjectName");
>>
So with a little script you can save the state of every object in your games:
void OnEnable(){
PlayerPrefs.SetInt(gameObject.name, 1);
}
void OnDisable(){
PlayerPrefs.SetInt(gameObject.name, 0);
}
void Start(){
if (PlayerPrefs.GetInt(gameObject.name) == 0)
gameObject.setActive(false);
}
Hope that will help you,
Raph
Answer by jameshutson108 · Nov 20, 2019 at 06:24 PM
Thank you @RadonRaph ! I really appreciate the response. I am trying to make this work but the PlayerPrefs do not quite work. For a start, I have multiple game objects in the game, but because they are children of other objects they all have the original prefab name so I cannot use the gameObject.name as an identifier. The other issue is that I need to remember only a number of objects to reset, that is the objects childed to an empty gameobject, and the spawnpoint is also childed. e.g. If a character dies he is returned back to the last spawnpoint, and all gameObjects after that spawn point need to be reset. Is there any way to do this? Thank you, James
Your answer
![](https://koobas.hobune.stream/wayback/20220612214921im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I'm trying to make a "Game Over" sound when the player dies and respawns 1 Answer
Getting an object to re-spawn/ transform player back to the beginning 1 Answer
2D 360 degress platformer example needed 0 Answers
Enemies respawn in platform game 1 Answer
2D Platformer - How do I have objects that kill the player upon touch/cause respawn? 2 Answers