PlayerPrefs Question - Prefs not activating until entry into the settings scene
Hi all, hope you are having a nice day. A bit of a noob question but despite a lot of googling, I cannot get this code to work.
Basically, I have a settings scene which can be accessed from the first main menu scene. Inside the settings there are the volume control sliders etc. When I enter play mode and enter the main menu, all my mixer groups will be the default (highest) value, and upon entry into the settings, the mixer groups will become their previously set values via playerprefs, and from then on the audio will behave.
The aim is to have the playerprefs kick in upon entering the first main menu scene and not need to go into the settings to activate them. I have tried 'getting' the playerprefs in the main menu scene but cannot seem to get it right. I will attach my two scripts, any help would be much appreciated.
Settings volume control script, attached to each of my volume sliders (I assume not the problem scripts).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;
using TMPro;
public class VolumeControlScript : MonoBehaviour
{
[SerializeField] string volumeParameter = "MasterVol";
public AudioMixer mixer;
[SerializeField] Slider slider;
[SerializeField] float multiplier = 30f;
[SerializeField] private TextMeshProUGUI sliderText;
void Awake()
{
slider.onValueChanged.AddListener(HandleSliderValueChanged);
}
void HandleSliderValueChanged(float value)
{
mixer.SetFloat(volumeParameter, Mathf.Log10(value)*multiplier);
}
void OnDisable()
{
PlayerPrefs.SetFloat(volumeParameter,slider.value);
}
void Start()
{
{
slider.onValueChanged.AddListener((v)=>
{
sliderText.text = (v*100).ToString("0");
});
}
slider.value=PlayerPrefs.GetFloat(volumeParameter,slider.value);
}
}
And the script I have attached the main camera in the main menu scene, trying to call the playerprefs(I assume this is the problematic script).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
public class VolumeControlInitial : MonoBehaviour
{
public AudioMixer masterMixer;
public float masterLvl;
public float musicLvl;
public float effectsLvl;
void Start()
{
PlayerPrefs.GetFloat("MasterVol");
PlayerPrefs.GetFloat("MusicVol");
PlayerPrefs.GetFloat("EffectsVol");
}
public void SetMasterVolume ()
{
masterLvl = PlayerPrefs.GetFloat("MasterVol");
masterMixer.SetFloat("MasterVol", masterLvl);
}
public void SetMusicVolume()
{
musicLvl = PlayerPrefs.GetFloat("MusicVol");
masterMixer.SetFloat("MusicVol", musicLvl);
}
public void SetEffectsVolume()
{
effectsLvl = PlayerPrefs.GetFloat("EffectsVol");
masterMixer.SetFloat("EffectsVol", effectsLvl);
}
}
Thanks again
Posted Nov 23, 2021, 2:50 AM UTC
This part does nothing since you are getting the values form PLayerPrefst, but not assigning them to anything.
...and after that you have a bunch of functions that would get the values again form PlayerPrefs, and assign them, but you aren't calling those functions anywhere.
So what you'd probaly want to do is replace that Start wiht calls to your functiosn instead:
Thank you for this, helped me fix it