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 /
  • Help Room /
This question was closed Dec 10, 2015 at 03:29 AM by LexGear for the following reason:

The issue was with the editor itself, deleting player prefs file fixed the issue.

avatar image
0
Question by LexGear · Oct 25, 2015 at 09:06 AM · editorbuildplayerprefsvolumedefault

PlayerPrefs defaulting to 0

Hi Guys, I can't seem to fix this simple problem. When testing in editor or running a build for the first time, the music volume and SFX volume keeps defaulting to 0. This is my code. Am I doing something wrong?

 function Start() {
     //Set SFX and Music Sliders to correct positions.
     if(!PlayerPrefs.HasKey("musicVolume")) PlayerPrefs.SetFloat("musicVolume",1);
     if(!PlayerPrefs.HasKey("sfxVolume")) PlayerPrefs.SetFloat("sfxVolume",1);
     var setMusicVolume = GameObject.Find("MusicSlider").GetComponent(UnityEngine.UI.Slider);
     setMusicVolume.value = PlayerPrefs.GetFloat("musicVolume");
     var setSfxVolume = GameObject.Find("SfxSlider").GetComponent(UnityEngine.UI.Slider);
     setSfxVolume.value = PlayerPrefs.GetFloat("sfxVolume");

}

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

4 Replies

  • Sort: 
avatar image
2

Answer by newtquestgames · Nov 07, 2015 at 08:57 PM

I had a problem like this because the slider's On Value Changed was set to "Editor and Runtime".

What happened was this; after playing the scene the editor would display the slider in the scene window with a value of 0. This triggered my On Value changed handler (nb: Editor and Runtime) which would then save the editor's 0 value for the slider into the player prefs.

Fix: Set the slider's On Value Change to Runtime Only.

I discovered this by using logging. The value 0 was always saved to player prefs when I stopped running the game to return to the editor.

Comment
Add comment · Show 1 · 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 LexGear · Nov 12, 2015 at 05:17 AM 0
Share

newtquestgames I think you might be on to something. Thanks, I'll check it out, this could be the problem.

avatar image
-1

Answer by Dinosaurs · Oct 25, 2015 at 04:16 PM

It might be that you're passing an integer; try passing 1f or 1.0 to SetFloat.

Comment
Add comment · Show 1 · 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 Statement · Nov 10, 2015 at 02:54 PM 0
Share

No. It's not passing an integer. The integer is converted to a float at compile time. The executing code never sees 1 as an integer, but as a float.

Consider the following example:

 public sealed class Example : $$anonymous$$onoBehaviour
 {
     void Start()
     {
         printFloat(1.0f); // Note 1.0 would be an error because that's a double.
         printFloat(1f);
         printFloat(1);
     }
 
     void printFloat(float value)
     {
         print(value);
     }
 }

It calls printFloat, which accepts a float value. I am calling it three times, once per variation discussed (1.0f, 1f, 1). Inspecting the compiled assembly in ILspy, we can see what the compiler did:

 public sealed class Example : $$anonymous$$onoBehaviour
 {
     private void Start()
     {
         this.printFloat(1f);
         this.printFloat(1f);
         this.printFloat(1f);
     }
 
     private void printFloat(float value)
     {
         $$anonymous$$onoBehaviour.print(value);
     }
 }

The reverse compilation of the IL instructions to C# showed that it compiled the equivalent of using 1f for all three cases. We can also look deeper into the IL instructions for Start only to sanity check what actually goes on.

 .method private hidebysig
     instance void Start() cil managed
 {
     // $$anonymous$$ethod begins at RVA 0x23e0
     // Code size 34 (0x22)
     .maxstack 8
 
     IL_0000: ldarg.0
     IL_0001: ldc.r4 1
     IL_0006: call instance void Statement.Example::printFloat(float32)
     IL_000b: ldarg.0
     IL_000c: ldc.r4 1
     IL_0011: call instance void Statement.Example::printFloat(float32)
     IL_0016: ldarg.0
     IL_0017: ldc.r4 1
     IL_001c: call instance void Statement.Example::printFloat(float32)
     IL_0021: ret
 } // end of method Example::Start

Before each call to printFloat, the implicit "this" reference and a constant real4 (that is a float - real number, 4 bytes long) is loaded on the stack and the method is executed. It'll make no difference what so ever adding a floating point suffix - the compiler can infer that from usage (the method has a float parameter).

(If you are eagle eyed, you see the method called is "Statement.Example::printFloat". I put all code in a namespace called "Statement" but excluded that from the example code to preserve line width. Functionally, it makes no difference.)

avatar image
0

Answer by Statement · Oct 25, 2015 at 04:32 PM

 if(!PlayerPrefs.HasKey("sfxVolume")) PlayerPrefs.SetFloat("sfxVolume",1);

This will only set the float to 1 if the key does not exist. If the key exists (if you set it to 0 some time ago for example), it will do nothing.

 setSfxVolume.value = PlayerPrefs.GetFloat("sfxVolume");

This will get the value, which was previously stored as 0.

You can clear playerprefs with DeleteKey or DeleteAll.

A nicer way to specify a default value, is to provide one when calling GetFloat.

 setSfxVolume.value = PlayerPrefs.GetFloat("sfxVolume", 1);

Note that it'll still return 0 until you delete the previous value you stored there.

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 LexGear · Dec 10, 2015 at 03:28 AM

So it turns out the problem was with the editor. Finding and deleting the player prefs file fixed this issue for me.

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

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

Why are my textures misaligned in the standalone build? 1 Answer

Why does my shoot script break when I build the game 0 Answers

Key Binds not saved on game play... 0 Answers

Value returns to default upon exiting the game 0 Answers

How to change the project's Splash Image in a build? 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