• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by GimLee · Aug 11, 2014 at 03:00 AM · checkpoint

Checkpoint automatic save/load

I am making a 2D sidescroller game. When the player walks over a checkpoint I want this position to be stored as the spawning position, after the next death.

I don't want to set all variables back to their original values, whenever a death occurs.

What I really want is to save this exact game-state when you pass the checkpoint. And then if player dies --> load state --> change playerPosition, set lives--.

What is the best way to do this? :)

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by thornekey · Aug 11, 2014 at 03:15 AM

PlayerPrefs is the best option for you. Heres an example for their lives:

 public int livesLeft = 3;
 
 void OnGUI () {
     if (livesLeft == 0) {
        if (GUI.Button (new Rect (Screen.width / 2 - 50, Screen.height / 2 + 50, 100, 25), "SAVE")) {
             
        PlayerPrefs.SetString ("Player Lives", livesLeft);
        }
 
        if (GUI.Button (new Rect (Screen.width / 2 - 50, Screen.height / 2 + 50, 100, 25), "SAVE")) {
            livesLeft == PlayerPrefs.GetString ("Player Lives");    
        }
     }
 }

   

It would be similar for their position too.

You could actually call a function (say its called RespawnPlayer) if the lives equal zero then do the load save.

Comment
GimLee

People who like this

1 Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GimLee · Aug 11, 2014 at 03:41 AM 0
Share

Hmm ok, PlayerPrefs you say. But how can I do the actual saving/loading.

avatar image GimLee · Aug 11, 2014 at 03:46 AM 0
Share

I can't see the difference between setting a string in playerprefs and just creating a normal string.

This seems to be saving the position of all enemies and the player in variables - and then reposition the objects if the player dies.

avatar image GimLee · Aug 11, 2014 at 03:50 AM 0
Share

Ok I see, Stores and accesses player preferences between game sessions. This is not what I need though. I want ALL the values in the scene to be reset, EXCEPT FOR TWO. playerPosition and Lives should NOT be reset, but everything else

avatar image thornekey · Aug 11, 2014 at 04:16 AM 0
Share

When you close your game, and re open it click load and it will load the player prefs of how many lives you have left. test it out. This is what you want. Even i see that lol. It will only save the lives and position of the player if you set it to do that.

avatar image thornekey · Aug 11, 2014 at 04:21 AM 0
Share

you dont have to have it in a button. Maybe make it so that it sets the player prefs when you go through a collider (a checkpoint).. then IF you reach 0 lives, load the player prefs..

 public int livesLeft = 3;
  
 void Update () {
     if (livesLeft == 0) {
        PlayerPrefs.GetString ("Player Lives");
        ////load the location player prefs float (PlayerPrefs.GetFloat)
  
     }
        
     if (collision with the collider checkpoint) {
         PlayerPrefs.GetString ("Player Lives", livesLeft);
         //save the checkpoint location in a float
     }
 }
Show more comments
avatar image

Answer by Kiwasi · Aug 11, 2014 at 04:07 AM

Two general strategies exist

  • Store the data on player lives and position

  • Reload the level (This will reset everything)

  • Move the player to the appropriate position

or

  • Store the state of every variable at the check point

  • On the players death move reset everything back to the checkpoint state

Check out the tutorial on saving and persistent data for methods to save.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Auto-save/Checkpoint system that you can load when restarting the game 1 Answer

how to make a checkpoints on unity for one player 1 Answer

Ok, new respawn and checkpoint system script issues 0 Answers

How to save the spawnpoint to the checkpoint by restarting the scene? 1 Answer

Teleporting 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges