i have an object contains the main music audio source and i want to reduce the sound volume within the game (when i go to game scene ) . how i can controlling in Audio volume from other scene and other script .
sorry for english
my code :
static var AudioBegin = true;
static var AudioStop = false;
function Awake()
{
if (AudioBegin)
{
audio.Play();
DontDestroyOnLoad(gameObject);
AudioBegin = false;
}
}
function Update(){
if (AudioStop)
{
audio.Stop();
}
}
Use PlayerPrefs to store desired volume in settings scene and when load it in game scene.
// somewhere in your settings scene
PlayerPrefs.SetFloat("SFX Volume", mySoundVolume);
// script on some game object in your game scene
using UnityEngine;
public class SettingsLoader : MonoBehaviour
{
// you should drag your AudioSource component
// on this field in inspector
public AudioSource audioSource;
void Start ()
{
audioSource.volume = PlayerPrefs.GetFloat("SFX Volume", 1f);
}
}
You are doing DontDestroyOnLoad(gameObject); in the awake function. So that means your game object is not destroyed when you load another scene.
To lower the volume, create a function:
function reduceVolume()
{
audio.volume = 0.3f;
}
And call it like you would call any other gameobject function.