Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save codehoose/63eef427cc9e7403f695b3a76f0975fc to your computer and use it in GitHub Desktop.
Save codehoose/63eef427cc9e7403f695b3a76f0975fc to your computer and use it in GitHub Desktop.
Slider Volume updation after returning to main menu from a different scene
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SliderVolumeValue : MonoBehaviour
{
const float DefaultVolume = 1f;
// Reference to Audio Source component
private AudioSource audioSrc;
private void Awake()
{
audioSrc = GetComponent<AudioSource>();
var volume = PlayerPrefs.GetFloat("musicVol", DefaultVolume);
audioSrc.volume = volume;
}
// Method that is called by slider game object
// This method takes vol value passed by slider
// and sets it as musicValue
public void SetVolume(float vol)
{
PlayerPrefs.SetFloat("musicVol", vol);
PlayerPrefs.Save();
audioSrc.volume = vol;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment