Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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 this:

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

Something like this?

 public void GoTimer(int amount) {
 timer = amount;
 }
 
 PlayerPrefs.SetInt("timeVar", timer); //save it like this
 int a = PlayerPrefs.GetInt("timeVar"); //get it like this
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 this 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 this 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 this 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 timerBoolFive$$anonymous$$in = false; //Timer 5 $$anonymous$$
 public bool timerBoolTen$$anonymous$$in = false; //Timer 10 $$anonymous$$


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 $$anonymous$$e! 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 ins$$anonymous$$d 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! $$anonymous$$ajor brain fart on my part there.

Saving would go something like:

         PlayerPrefs.SetInt("timerBoolSixty", timerBoolSixty); //or 1 ins$$anonymous$$d of the variable
         PlayerPrefs.SetInt("timerBoolFive$$anonymous$$in", timerBoolFive$$anonymous$$in); // or 0 ins$$anonymous$$d of the variable
         PlayerPrefs.SetInt("timerBoolTen$$anonymous$$in", timerBoolTen$$anonymous$$in); // or 0 ins$$anonymous$$d of the variable

You could always rename them to "int" ins$$anonymous$$d 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 :) O$$anonymous$$ 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 timerBoolFive$$anonymous$$in = false; //Timer 5 $$anonymous$$.
     public bool timerBoolTen$$anonymous$$in = false; //Timer 10 $$anonymous$$.
     public bool timerBoolTwenty$$anonymous$$in = false; //Timer 20 $$anonymous$$.
     public bool timerBoolThirty$$anonymous$$in = false; //Timer 30 $$anonymous$$.
     public bool timerBoolFourty$$anonymous$$in = false; //Timer 40 $$anonymous$$.
     public bool timerBoolFifty$$anonymous$$in = false; //Timer 50 $$anonymous$$.
     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 $$anonymous$$IN
 *300 // 5$$anonymous$$IN
 *600 // 10 $$anonymous$$IN
 *3600 // 1 HOUR
 * ETC...
 */

These are comments to remember yourself. $$anonymous$$akes 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.

Follow this Question

Answers Answers and Comments

37 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 avatar image

Related Questions

Resetting Timer Using PlayerPrefs? (JS) 1 Answer

Issues with Save / Load system 1 Answer

Playerprefs doesn't save anything. 2 Answers

How do you save timer with PlayerPrefs? 1 Answer

timer not ticking down 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