- Home /
Changing Music Volume = Calling a function in another script in another scene
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 ins$$anonymous$$d 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.
Answer by flaminghairball · Aug 22, 2011 at 08:07 PM
Given that it's a global music player, you'll probably want to make it a static thing, and make MusicVolume() a static function.
Answer by aldonaletto · Aug 22, 2011 at 08:55 PM
You must find the object at which the script is attached then access the script with GetComponent(BackgroundMusic). Supposing the object name is "Jukebox", you can do the following:
var juke = GameObject.Find("Jukebox"); // find the object
juke.transform.GetComponent(BackgroundMusic).MusicVolume();
But if you want to allow volume control at any point of your game, add this slide control to your script BackgroundMusic.js - it will show the slider all the time:
private var curVolume: float;
function Start(){ MusicVolume(); // set the initial volume from PlayerPrefs curVolume = audio.volume; // copy it to curVolume }
function OnGUI(){ // this slider will be shown all the time in the screen curVolume = GUI.HorizontalSlider (Rect (25, 25, 100, 30), curVolume, 0.0, 1.0); audio.volume = curVolume; }
Thanks for your examples. However, I get a NullReferenceExeption Error at juke.transform.GetComponent(Background$$anonymous$$usic).$$anonymous$$usicVolume(); when running the script. Any idea whats wrong?
Seems the object can't be found. Using this code: var juke = GameObject.Find("Jukebox"); // find the object if (juke == null) {print("No object found with that name!");} else {print("someObject name: " + juke.name); I get "No object found with that name!" The object is called "Jukebox", so it does exist. Why can't it be found?
It's very strange: GameObject.Find finds any active object with the specified name, even if it's childed to other objects (Transform.Find doesn't). The only reason I can think for this to fail is this code being used at Awake - Awake occurs during object initialization, thus other objects may not exist yet. If this is the case, move the code to Start.
Anyway, you can place a GameObject variable in the script where the volume control is and drag your music object to it - something like:
var jukeBox: GameObject; // drag your jukebox here
function SetVolume(vol: float){ jukeBox.audio.volume = vol; // set directly the jukebox volume }
function ResetVolume(){ // restore the PlayerPrefs volume jukeBox.transform.GetComponent(Background$$anonymous$$usic).$$anonymous$$usicVolume(); }
Your answer
Follow this Question
Related Questions
adjust the slider to control the volume 1 Answer
Multiple, independent volume sliders 1 Answer
Change volume of objects with a tag 1 Answer
Need help with my slider 3 Answers
Music Volume 2 Answers