Why does my function play sounds on startup?
Asked Answered
U

5

0

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, "");
    }

    //...

}
Unsuccessful answered 28/1 at 14:30 Comment(0)
D
0

Have you checked to see if your audio source has “Play On Awake” disabled? I believe it’s enabled by default.

Divestiture answered 17/7, 2012 at 6:9 Comment(1)

Thanks Man... It worked for me!!!

Did
V
0

Turn off “Play on awake” from the inspector. Your function isn’t the problem.

Voluble answered 17/7, 2012 at 7:12 Comment(0)
U
0

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.

Unsuccessful answered 17/7, 2012 at 21:7 Comment(0)
C
0

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.

Cuesta answered 17/7, 2012 at 23:48 Comment(0)
P
0

Having a similar problem. But when I disable “play on wake up”, the sounds will not play at all later on, even in the scripts that are supposed to play them.

If “play on wake up” is enabled, sounds play on startup and also when triggered in a script as intended.

Panzer answered 28/1 at 12:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.