Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by NickP_2 · Jun 07, 2014 at 05:01 PM · playerprefsfloatvaluesdefault

If PlayerPrefs.GetFloat("name") don't excist

quoted from unity scripting API for PlayerPrefs.GetFloat("keyname"):

Returns the value corresponding to key in the preference file if it exists. If it doesn't exist, it will return defaultValue.

Now I looked up the default value of a float, which should be 0.0f. But the folowing function doesn't return true:

 //function in class named SaveOpenData
 public static float GetPerkCost(string name)
 {
     return PlayerPrefs.GetFloat (name + "PerkCost");
 }


 //check statement        
 if(SaveOpenData.GetPerkCost(name) == 0.0f)
 {
     Debug.Log("Empty");
 }

before the if statement I do:

 PlayerPrefs.DeleteAll();


I also tried to compare it with null or just 0. What am I doing wrong

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

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by LMan · Jun 07, 2014 at 05:11 PM

I believe you are looking for PlayerPrefs.HasKey!

Comment
Add comment · Show 2 · 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 NickP_2 · Jun 07, 2014 at 05:14 PM 0
Share

Woa, overlooked this... I always make things more complicated... Thanks!

avatar image LMan · Jun 07, 2014 at 05:20 PM 0
Share

Haha me too man, glad I could help!

avatar image
3

Answer by Eric5h5 · Jun 07, 2014 at 05:22 PM

You need to supply a default value. It's not about what the default for a float is:

 PlayerPrefs.GetFloat (name + "PerkCost", 0.0f);

That way the default will be 0.0 if the PlayerPrefs entry doesn't exist, and saves having to write extra code with HasKey and so on.

Comment
Add comment · Show 2 · 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 NickP_2 · Jun 09, 2014 at 01:40 AM 0
Share

Oh. They don't mention that in the API...(well not that I know.) Thanks for the comment, always happy to learn!

avatar image Eric5h5 · Jun 13, 2014 at 05:16 PM 0
Share

Yes, it's mentioned in the API docs. You quoted it yourself: "Returns the value corresponding to key in the preference file if it exists. If it doesn't exist, it will return defaultValue." That has nothing to do with the default value of floats in general; not sure why you'd interpret it that way since defaultValue is written like that to specifically refer to defaultValue in the parameter list:

 static function GetFloat(key: string, defaultValue: float = 0.0F): float;
avatar image
0

Answer by elenzil · Jan 27 at 06:02 PM

The unity docs here are incomplete, if you ask me. They seem to not describe the first flavor of the routine.

However, my tests don't match the OP's results. For me, the following code shows that the first flavor of GetFloat() returns 0 if the key is not found:

         var noSuchKey = System.Guid.NewGuid().ToString();
         float f = PlayerPrefs.GetFloat(noSuchKey);
         Debug.Log($"PlayerPrefs['{noSuchKey}'] = {f}");
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

24 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

Related Questions

Problems with pulling a slider value - (Making sensitivity Sliders) 0 Answers

Changing gameobjects Float from another script? 1 Answer

performance wise, which is better..Playerprefs or global floats 3 Answers

Getting a slider value from a different scene 1 Answer

Is there a way to "Apply" transformations on an object without simply attatching it to an empty gameobject? 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