Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

PlayerPrefs Issue

Discussion in 'Scripting' started by Toggleguy, Dec 30, 2017.

  1. Toggleguy

    Toggleguy

    Joined:
    Dec 13, 2016
    Posts:
    7
    Hi all, im trying to store the volume levels of my game using PlayerPrefs (SFX and Music), however, it seems as if only one will work at a time. It may be something simple because im an amature to unity, but I really cant figure it out. I have the following 2 scripts attached to a sound object, one for SFX and Music(they're near enough identical bar the PlayerPref and variable names):

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class SFXVolUpdate : MonoBehaviour
    7. {
    8.  
    9.     public Slider SFXslider;
    10.     public AudioSource SFXsource;
    11.     // Use this for initialization
    12.     void Start()
    13.     {
    14.             SFXslider.value = PlayerPrefs.GetFloat("SFXVolume");
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         SFXsource.volume = SFXslider.value;
    21.     }
    22.     public void OnClick()
    23.     {
    24.         PlayerPrefs.SetFloat("SFXVolume", SFXslider.value);
    25.     }
    26. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class MusicVolUpdate : MonoBehaviour
    7. {
    8.  
    9.     public Slider Musicslider;
    10.     public AudioSource Musicsource;
    11.     // Use this for initialization
    12.     void Start()
    13.     {
    14.         Musicslider.value = PlayerPrefs.GetFloat("MusicVolume");
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         Musicsource.volume = Musicslider.value;
    21.     }
    22.     public void OnClick()
    23.     {
    24.         PlayerPrefs.SetFloat("MusicVolume", Musicslider.value);
    25.     }
    26. }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Are there 2 audio sources on the game object? Besides that, I don't see why just one would work and not the other.

    I would recommend that you consider using the slider's "OnValueChanged" event to change the volume, as opposed to polling it in Update (just good practice).
     
    MickM likes this.
  3. Toggleguy

    Toggleguy

    Joined:
    Dec 13, 2016
    Posts:
    7
    I've tried it with both 1 and 2 audio sources on the object, but the same result is observed
    If I have both scripts enabled, the value of the SFX slider sets to 1, and the value of the Music slider sets to 0 every time. But if only 1 is enabled, it will work correctly for that slider
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hm. that is strange. Do you have 2 sliders, yes? And you dragged the proper references in there (to the script)?
     
  5. Toggleguy

    Toggleguy

    Joined:
    Dec 13, 2016
    Posts:
    7
    I do have 2 sliders yes, and they are assigned to the proper variables.
    Upon further testing I foud that:
    -With SFX script only enabled, the sfx sider and audio function perfectly, and so does the PlayerPrefs
    -With the SFX and Music scripts both enabled, SFX slider = 1 and Music slider = 0 upon every relaunch
    -With Music script only enabled, SFX slider = 1 and Music slider = 0 upon every relaunch
    This makes me think its something to do with the Music vol script?
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Not sure what is going wrong. Good to know you have the sliders and proper variables (doesn't hurt to check).

    Can't say this will give you comfort, but if I were you, I'd get my float values from player prefs and Debug.Log their values to the console to read. Make sure they are right/working.
     
  7. MickM

    MickM

    Joined:
    Nov 19, 2012
    Posts:
    166
    Given they are the same scripts with different variable names, if there is a functionality bug it is likely inspector related setup. Can you post a screenshot of the setup?

    Also you could generalise that script by adding a string variable for the playerPrefString
     
  8. Toggleguy

    Toggleguy

    Joined:
    Dec 13, 2016
    Posts:
    7
    @MickM @methos5k Heres the screenshots (I edited them to show the situation more clearly). Dont worry about the extra script in the Audio Object (I basically combined the 2 scripts into one and found that the slider with the
    Code (CSharp):
    1. Musicslider.value = PlayerPrefs.GetFloat("MusicVolume");
    line chronologically first in the program would work as intended, however the second would always be set to 1). Using the console I found that in this script, the values of both PlayerPrefs would be valid if both are checked at start(); However if I was to use the GetFloat command on one, it would set the other to 1. Here's the code for reference:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ChangeVol : MonoBehaviour {
    7.  
    8.     public Slider SFXslider;
    9.     public AudioSource SFXsource;
    10.  
    11.     public Slider MUSICslider;
    12.     public AudioSource MUSICsource;
    13.  
    14.     void Start () {
    15.             Debug.Log(PlayerPrefs.GetFloat("SFXVolume"));               //These values are as they should be
    16.             Debug.Log(PlayerPrefs.GetFloat("MUSICVolume"));             //^
    17.             MUSICslider.value = PlayerPrefs.GetFloat("MUSICVolume");
    18.             Debug.Log(PlayerPrefs.GetFloat("SFXVolume"));               //This now = 1, for some strange reason
    19.             SFXslider.value = PlayerPrefs.GetFloat("SFXVolume");
    20.             Debug.Log(PlayerPrefs.GetFloat("MUSICVolume"));        
    21.     }
    22.  
    23.     void Update ()
    24.     {
    25.         SFXsource.volume = SFXslider.value;
    26.         MUSICsource.volume = MUSICslider.value;
    27.     }
    28.  
    29.     public void OnClick()
    30.     {
    31.         PlayerPrefs.SetFloat("SFXVolume", SFXslider.value);
    32.         PlayerPrefs.SetFloat("MUSICVolume", MUSICslider.value);
    33.     }
    34. }
     

    Attached Files:

  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Getting a float value from player prefs shouldn't in any way be setting a different value to 1 just arbitrarily :)

    This may sound silly, but are you sure the slider's not just set to 1 when you start? :)

    I'm a little lost as to what's wrong. Maybe I'm overlooking/forgetting something simple..

    If the values come out correctly from player prefs, then setting the slider to said value does what? -- should work!? :)
     
    Toggleguy likes this.
  10. Toggleguy

    Toggleguy

    Joined:
    Dec 13, 2016
    Posts:
    7
    OK, managed to find the issue. I figured that maybe the player perfs were struggling with the non integer numbers, so I checked the 'Whole Numbers' box on my sliders component, and set the max to 100 - Please see attached photo for future reference. Thanks you guys for your time, I'm going to make a brew after trying to figure that out for 4 hours. :0
     

    Attached Files:

  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No doubt, I'm glad that it's working for you.

    I do not know what was really wrong, but it doesn't "struggle" for those values. :)
    It's cool, though, I'm just adding that so that , if some day in the future you want to store & load a float, you won't be scared. For now, it's cool that it's working with whole numbers. :)

    Take it easy. heh
     
    Toggleguy likes this.
  12. MickM

    MickM

    Joined:
    Nov 19, 2012
    Posts:
    166
    Your music slider button was calling the SFXVolUpdate OnClick method.
     
  13. Toggleguy

    Toggleguy

    Joined:
    Dec 13, 2016
    Posts:
    7
    Yea, I just did this to test it, but thanks anyway. :)