Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
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
Add comment
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
1
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
Add comment · 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. $$anonymous$$aybe 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
0

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
Add comment · 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.

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

2D Game Checkpoint System 1 Answer

Checkpoints in a platformer 1 Answer

Checkpoint in c# 0 Answers

save funtion c# 1 Answer

problems with check point in unity 0 Answers


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