Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fairinrenish/da233147657e563d45ac3f7a8ffde398 to your computer and use it in GitHub Desktop.
Save fairinrenish/da233147657e563d45ac3f7a8ffde398 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 {
static SliderVolumeValue instance = null ;
private void Awake()
{
if (instance != null)
{
Destroy (gameObject);
}
else
{
instance = this;
DontDestroyOnLoad (gameObject);
}
}
// Reference to Audio Source component
public AudioSource audioSrc;
// Music volume variable that will be modified
// by dragging slider knob
public float musicVolume = 0f;
// Use this for initialization
void Start()
{
// Assign Audio Source component to control it
audioSrc = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
// Setting volume option of Audio Source to be equal to musicVolume
audioSrc.volume = musicVolume;
PlayerPrefs.GetFloat("musicVol", musicVolume);
}
// 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)
{
musicVolume = vol;
PlayerPrefs.SetFloat("musicVol", musicVolume);
PlayerPrefs.Save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment