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 GhostDre · Apr 01, 2015 at 01:19 PM · updatesavingstartlives

Saving Life And Updating in Game

Hi everyone, Im running into a slight problem. Im able to save lives when the game is over or when you are returning to the main menu but when I start the game the lives that were save do not update. But start at 20 again. I know it has to do with the Start being incorrect but I don't know how to have the Updated lives show instead of the Start lives. I want to show current lives that has been loss or gained. Any suggestions?

GameController.cs

       //This is were I want to start off the game with Up-To_Date lives
 
         void Start(){
              life = 20;
               SavingLives ();
         }
 
 
 //This is where I am saving the lives
     public void SavingLives()
     {
         
         if (life >=0) {
             
             PlayerPrefs.SetInt("life",  life );
             PlayerPrefs.Save();
         }
 
     }

MenuContoller.cs //This calls the saved lives to the main menu and saves and displays the current lives.

 void Start () {
         lifeUpdateText.enabled = true;
 
     }
 
     void Awake(){
         lifeUpdateText.text = PlayerPrefs.GetInt ("life").ToString ("");
         }
     
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
0

Answer by LSPressWorks · Apr 01, 2015 at 02:21 PM

Yeppers, it is the start function. Set a playerprefs value of newfile or something. If that is false then set the life to 20, otherwise load the life up.

You also need to store the life value in an variable after loading, instead of just putting it into a string.

 localLife =  PlayerPrefs.GetInt;
 lifeUpdateText.text =localLife.ToString ("");


Stepping into your code, you're setting life to 20. Then saving that value to the file, overwriting the previous data. Then you are displaying the life.

in pseudoCode

 if(file IS_NEW) then SET_LIFE == 20 and set file to NOT_NEW
 else GET_LIFE and place into a variable
 DISPLAY_LIFE on updateText.text
 if(PLAYER_DEAD or QUIT_GAME) then SET_LIFE.


Comment
Add comment · Show 3 · 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 GhostDre · Apr 01, 2015 at 11:57 PM 0
Share

Im a bit confused with the pseudoCode, I believe I got it but I don't think I'm looking at it right. So I should make IS_NEW a bool and set it to true saying that if the game is new set the lives to 20 Right? How is the game suppose to know if it's new, I'm confused on setting this up sorry about that.

avatar image GhostDre · Apr 06, 2015 at 02:05 AM 0
Share

Still can anyone help me I am still lost here. Please help?

avatar image GhostDre · Apr 07, 2015 at 01:11 AM 0
Share

Please help someone?

avatar image
0

Answer by Priyanka-Rajwanshi · Apr 01, 2020 at 10:56 AM

You have been resetting life = 20; in the Start. Instead, it is advised to use the following in the Start function:

life = PlayerPrefs.GetInt("life", 20);

This way, whatever life count is last saved, the same is used again in the start function. Moreover, if you want to replenish lives after sometime, you can check out the following link: http://codesaying.com/life-counter-in-unity/

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Use my own update() and start() methods. 1 Answer

Why if I execute .SetText on the Start it dont works. 1 Answer

Where to save data so not overwritten on App Update? 0 Answers

GetComponent in the Update function? 1 Answer

Start being called after Update of another GameObject? 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