- Home /
Why does my function play sounds on startup?
Yo World!
I've made a GUI that has some buttons, and a sound attached to it. For some reason when I start the game, it opens all the subwindows and even plays the sounds, how do I stop it from doing this on startup?
function OnGUI ()
{
GUI.Box(Rect(Screen.width/2-133, 5, 266, 50),"");
if (GUI.Button(Rect(Screen.width/2-128,10,40,40),Interface_Hotbar_Image_01))
{
audio.PlayOneShot(Interface_Hotbar_Sound_01);
audio.Play();
if(Interface_01_Displaying)
Interface_01_Displaying = false;
else
Interface_01_Displaying = true;
}
if(Interface_01_Displaying)
{
Interface_01_Window = GUI.Window (1, Interface_01_Window, Interface_01_Settings, "");
}
...
Answer by dannyskim · Jul 17, 2012 at 06:09 AM
Have you checked to see if your audio source has "Play On Awake" disabled? I believe it's enabled by default.
Answer by xSpectrum · Jul 17, 2012 at 07:12 AM
Turn off "Play on awake" from the inspector. Your function isn't the problem.
Answer by kenkennet20 · Jul 17, 2012 at 09:07 PM
But what about the displaying of the rects, why does it open them from start, or how do I run a code afterwards to hide them.
Answer by VicM · Jul 17, 2012 at 11:48 PM
Hi, what do you mean by "it opens all subwindows"? Do you mean that it triggers the code inside each IF statetement even if they are not pressed? That would be weird definitely.
On work around is to use an inspector variable (global one) and use it as a flag to enter or not to your GUI drawing functions (code inside the GUI function) If the conditions you set are meet then set the variable to TRUE and you will get your GUI.
However, is just a workaround and not an elegant one, maybe there is something in the code that is causing to enter to the IF statements, if that is the case.
Your answer
Follow this Question
Related Questions
Audio sounds accelerated when starting webplayer??? 1 Answer
java.lang.IllegalStateException: stop() called on uninitialized AudioTrack. 1 Answer
How to send a signal when something stops happening? 3 Answers
Unity Crashes on Startup - Sound Driver Issue? DxDiag Provided 0 Answers
Automatic repeated sound 1 Answer