My slider properties are missing when i switch scene from the main menu. Why does that happen ?
Here's my code on the main menu where my Slider is:
using UnityEngine;
using UnityEngine.UI;
public class AudioManager : MonoBehaviour
{
private static readonly string FirstPlay = "FirstPlay";
private static readonly string MusicPref = "MusicPref";
private static readonly string SoundPref = "SoundPref";
private int firstPlayInt;
public Slider musicSlider, soundSlider;
private float musicFloat, soundFloat;
public AudioSource musicAudio;
public AudioSource[] soundAudio;
private static AudioManager instance = null;
void Start()
{
firstPlayInt = PlayerPrefs.GetInt(FirstPlay);
if (firstPlayInt == 0)
{
musicFloat = .125f;
soundFloat = .75f;
musicSlider.value = musicFloat;
soundSlider.value = soundFloat;
PlayerPrefs.SetFloat(MusicPref, musicFloat);
PlayerPrefs.SetFloat(SoundPref, soundFloat);
PlayerPrefs.SetInt(FirstPlay, -1);
}
else
{
musicFloat = PlayerPrefs.GetFloat(MusicPref);
musicSlider.value = musicFloat;
soundFloat = PlayerPrefs.GetFloat(SoundPref);
soundSlider.value = soundFloat;
}
}
public void SaveSoundSettings()
{
PlayerPrefs.SetFloat(MusicPref, musicSlider.value);
PlayerPrefs.SetFloat(SoundPref, soundSlider.value);
}
void OnApplicationFocus(bool inFocus)
{
if (!inFocus)
{
SaveSoundSettings();
}
}
public void UpdateSound()
{
musicAudio.volume = musicSlider.value;
for (int i = 0; i < soundAudio.Length; i++)
{
soundAudio[i].volume = soundSlider.value;
}
}
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
return;
}
if (instance == this) return;
{
Destroy(gameObject);
}
}
}
and here's my code on the other scene :
using UnityEngine;
public class AudioSetting : MonoBehaviour
{
private static readonly string MusicPref = "MusicPref";
private static readonly string SoundPref = "SoundPref";
private float musicFloat, soundFloat;
public AudioSource musicAudio;
public AudioSource[] soundAudio;
private void Start()
{
Destroy(FindObjectOfType());
}
private void Awake()
{
ContinueSettings();
}
private void ContinueSettings()
{
musicFloat = PlayerPrefs.GetFloat(MusicPref);
soundFloat = PlayerPrefs.GetFloat(SoundPref);
musicAudio.volume = musicFloat;
for(int i = 0; i < soundAudio.Length; i++)
{
soundAudio[i].volume = soundFloat;
}
}
}
↧