Restarting a level and keeping the data
I was trying to make a level with more than just one life (meaning 3). I managed to do the scripting, the health system works fine (I did it with the help of a verified youtuber, so it sure isn't wrong). The problem is, that everytime the player dies, the scene restarts, and altough a little heart disappears, after restarting the level it appears again as if the player never died. My question is, how can I solve the problem of transporting the data to the restarted level? Or is there any other way to restart the level that saves the information acquired before? Thank you, and have a nice day! (btw I really am a beginner, most of the things I did was watching videos and trying to duplicate them/make my own version out of it. so pretty please, give an answer that a noob like me could understand as well. Thank you! :))
Answer by streeetwalker · May 01, 2020 at 08:30 PM
@dancus25_unity, Are you using the Scene Manager to reload the scene? You'll have to save any data you want to retain either to a Static class that is not attached to any game object (and does not inherit from MonoBehavior), or set up a Singleton on a gameObject that is set to DoNotDestroy.
That means you'll also need to have code in your Scene startup that will check how many hearts to show based on your current life.
A better way is to not reload the scene, just wipe your level changes back to the their starting values, except of course for the hearts.
I tried teleporting the player back to the starting point (transform.position.x = 0), but I started to experience some bugs. Did I do the teleportation part wrong? @streeetwalker I am using the Scene $$anonymous$$anager to reload the scene. Sadly, I am such a noob that I don't understand the first part, plus that I'm not a native English speaker :( so could you please reiterate?
Your answer
Follow this Question
Related Questions
Referencing playerHealth (from a separate script) in an if statement 1 Answer
What are the parameters for OnMouseOver and how can I use them? 0 Answers
What is wrong with my code...watched an unity tutorial and I keep forgetting everything! 1 Answer
How to destroy a gameobject before instantiating another one in its location? 1 Answer