Question by
emirkutluay · Apr 26, 2020 at 10:20 AM ·
playerprefsaudiolistener
PlayerPrefs with audiolistener doesn't work.
I have this code to adjust audio with slider(which works) and it saves the slider value. But when I load the game again slider stays the same audio resets. What is wrong with the code?
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;
public class SetVolume : MonoBehaviour
{
public Slider sliderSound;
private void Start()
{
sliderSound = GetComponent<Slider>();
AudioSettings();
SliderSettings();
}
private void Update()
{
PlayerPrefs.SetFloat("SliderValue", sliderSound.value);
PlayerPrefs.SetFloat("AudioValue", AudioListener.volume);
}
public void SliderSettings()
{
if (PlayerPrefs.HasKey("SliderValue"))
{
sliderSound.value = PlayerPrefs.GetFloat("SliderValue");
}
else
{
sliderSound.value = 1f;
}
}
public void AudioSettings()
{
if (PlayerPrefs.HasKey("AudioValue"))
{
AudioListener.volume = PlayerPrefs.GetFloat("AudioValue");
}
else
{
AudioListener.volume = 1f;
}
}
public void OnValueChanged()
{
AudioListener.volume = sliderSound.value;
}
}
Comment
Your answer
Follow this Question
Related Questions
Executing Actions Through PlayerPrefs 1 Answer
Faster Way to Increment PlayerPrefs Int 1 Answer
Playerprefs, i dont know how to use 0 Answers
How do I use PlayerPrefs properly? 1 Answer
how i save and load my coins look the script please 0 Answers