Music plays in all scenes, but I can’t change the volume, except in the startup scene. I’m using the following JS to start the music in the startup screen of the game. The volume needs to be adjusted in the game’s setup scene. Just saying “audio.volume =” works here in the startup script, but does not work in the later setup scene. Obviously the compiler does not know which volume I want to change, and ignores my request. Wasn’t there a way to access, in this case, my function “MusicVolume” inside this script, which I named “BackgroundMusic”, from another JS in another scene? I just don’t know how to do this!
var backgroundMusic : AudioClip;
audio.clip = backgroundMusic;
audio.Play();
function Start()
{
MusicVolume();
//if(!PlayerPrefs.HasKey("MusicLevel")) { audio.volume = PlayerPrefs.GetFloat("MusicLevel");}
}
function MusicVolume()
{
if(!PlayerPrefs.HasKey("MusicLevel")) { audio.volume = PlayerPrefs.GetFloat("MusicLevel");}
}
Thanks to those who answered. However, the problem appears to be more complex than expected. Decided to circumvent the problem by not using DoNotDestroy, and instead playing the music only in the game scene. It will of cause stop when leaving the scene, and restart from the beginning when re-entering the scene.
– RolfBertram-com