• 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 KnightRiderGuy · Jan 24, 2016 at 09:38 PM · playerprefssavetimersaveloadtimer countdown

Save Time Button Settings in Player Prefs

I have a timer script where you can chose pre set time duration on the time with button methods like t$$anonymous$$s:

 //Timer Settings
     public void GoTimerSetting60Sec()
     {
         timer = 60;
     }
     
     public void GoTimerSetting5Min()
     {
         timer = 300;
     }
     
     public void GoTimerSetting10Min()
     {
         timer = 600;
     }

How would I go about saving a selected timer preset to player prefs so that the time would automatically be selected when the app is restarted?

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 ShadyProductions · Jan 24, 2016 at 09:48 PM

Somet$$anonymous$$ng like t$$anonymous$$s?

 public void GoTimer(int amount) {
 timer = amount;
 }
 
 PlayerPrefs.SetInt("timeVar", timer); //save it like t$$anonymous$$s
 int a = PlayerPrefs.GetInt("timeVar"); //get it like t$$anonymous$$s
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
avatar image
0

Answer by SterlingSoftworks · Jan 24, 2016 at 09:50 PM

You could set up PlayerPref bools for each one of these button types and then at the beginning of t$$anonymous$$s script (or wherever you're needing it) check each player pref bool, and the one that's true, set the time to the corresponding timer bool you made.

If you go t$$anonymous$$s route, make sure you set the other two to false when you mark one true.

 public void GoTimerSetting60Sec(){
     timer = 60;
     timerBoolSixty = true; //Place holder bool name
     timerBoolFiveMin = false; //Place holder bool name
     timerBoolTenMin = false; //Place holder bool name
 }

And etc etc.

Programming tip: Make t$$anonymous$$s all ONE function, it's more dynamic, is easier to edit and looks better (to me at least)

 public void GoTimerSetting(int time){
     timer =  time;
 }

Then in your buttons' OnClick() stuff just put in the amount in the box for the corresponding button.

Comment
Add comment · Show 21 · 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 KnightRiderGuy · Jan 24, 2016 at 10:05 PM 0
Share

Thanks @SterlingSoftworks So at the top of my script I would set up the bools for each timer button something like this right?

 public bool timerBoolSixty = false; //Timer 60
 public bool timerBoolFiveMin = false; //Timer 5 min
 public bool timerBoolTenMin = false; //Timer 10 min


I made them public just so I can see that they will change in the inspector. If I have that right on the saving to player prefs side of it I would do what?

avatar image ShadyProductions KnightRiderGuy · Jan 24, 2016 at 10:09 PM -1
Share

What do you need the bools even for, what is your main purpose of this timer?:P

avatar image SterlingSoftworks ShadyProductions · Jan 24, 2016 at 10:16 PM 0
Share

Dude I know! I totally brain farted there! Your way is probably better than mine! xD

Show more comments
avatar image SterlingSoftworks KnightRiderGuy · Jan 24, 2016 at 10:13 PM 0
Share

Oh shoot! I apologize so much! Bools are not part of PlayerPrefs! That's a huge mistake on my part!

What you could do is instead of bools you could jut have them be ints and have 0 be your "false" and 1 be your "true". I truly do apologize for the mistake! Major brain fart on my part there.

Saving would go something like:

         PlayerPrefs.SetInt("timerBoolSixty", timerBoolSixty); //or 1 instead of the variable
         PlayerPrefs.SetInt("timerBoolFiveMin", timerBoolFiveMin); // or 0 instead of the variable
         PlayerPrefs.SetInt("timerBoolTenMin", timerBoolTenMin); // or 0 instead of the variable

You could always rename them to "int" instead of "bool" too if it helps you not get confused by my stupid answer.

avatar image KnightRiderGuy SterlingSoftworks · Jan 24, 2016 at 10:23 PM 0
Share

Thanks @SterlingSoftworks, Ok little confused, sorry :) OK I can only play one timer at a time so lets say I choose timer button for 60 seconds and that was the last time setting I used so when I restart my app that would be the one I would want to be active.

But just to be sure I would NOT need these bools?

 //Timer Button Bools
     public bool timerBoolSixty = false; //Timer 60 Sec.
     public bool timerBoolFiveMin = false; //Timer 5 min.
     public bool timerBoolTenMin = false; //Timer 10 min.
     public bool timerBoolTwentyMin = false; //Timer 20 min.
     public bool timerBoolThirtyMin = false; //Timer 30 min.
     public bool timerBoolFourtyMin = false; //Timer 40 min.
     public bool timerBoolFiftyMin = false; //Timer 50 min.
     public bool timerBool1Hour = false; //Timer 1 Hour.
     public bool timerBool1Point5Hour = false; //Timer 1.5 Hour.
     public bool timerBool1TwoHours = false; //Timer 2 Hour.
     public bool timerBool1TwoPoint5Hours = false; //Timer 2.5 Hour.
     public bool timerBool1ThreeHours = false; //Timer 3 Hour.
Show more comments
avatar image ShadyProductions KnightRiderGuy · Jan 24, 2016 at 10:27 PM -1
Share

Well when you click the timer button, you would save the int to playerprefs.
Say I press the button I just do something like this:

 public void GoTimer(int amount) {
  timer = amount;
  PlayerPrefs.SetInt("timeVar", timer); //save the last used amount.
  }
  
 GoTimer(60); //call this on button press, it automaticly saves it to playerprefs
 //and in your new scene you can get it like so:
 
 int timer = PlayerPrefs.GetInt("timeVar");
 //timer will be 60

And no you won't need those bools lol. What you can do to remember the times is put something like this

 /*
 *60 // 1 MIN
 *300 // 5MIN
 *600 // 10 MIN
 *3600 // 1 HOUR
 * ETC...
 */

These are comments to remember yourself. Makes it easier.

avatar image KnightRiderGuy ShadyProductions · Jan 24, 2016 at 10:38 PM 0
Share

@ShadyProductions Thanks, would I not use something like this to enable my bools?

 //Timer Settings
     public void GoTimerSetting60Sec()
     {
         timer = 60;
         timerBoolSixty = true;
     }

And do something like what @SterlingSoftworks suggested and set all of the others to false and then save that button setting to prefs?

Show more comments
Show more comments

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

36 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 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

Resetting Timer Using PlayerPrefs? (JS) 1 Answer

Saving and displaying best time achieved using playerprefs 1 Answer

Why is player prefs not working? 1 Answer

Issues with Save / Load system 1 Answer

Playerprefs doesn't save anything. 2 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