- Home /
Disabled audio source problem on a panel popup
Hi everyone, I’m new to Unity, but I have searched everywhere and could not find an answer so I’m asking for your expertise. I have a canvas with a panel that I control in code when to display (popup). I have several buttons on the panel for the user to press and each button plays a sound (the buttons are children of the panel, which is a child of the canvas). At the beginning of the game, I hide my panel (setactive = false). Then I display it using setactive=true later on in the game. My problem is that the audio does not play when the user presses a button. I’m using playatpoint and I get no error (play one shot is not created though). When I try to use play() just to test it out, the console says “cannot play a disabled audio source”. When the panel loads at the beginning of the game, I put a playatpoint at the start function just to test to see if I can hear a sound and I can. It’s only when I hide the panel and then display the panel (using setactive on the panel) that it doesn’t work. Does anyone have any ideas? I’ve been stuck on this for many hours and many days. I’ve checked audio listener and it is enabled, I’ve checked all my audio sources and they are enabled. Other than the popup, my sounds play during my game. Thanks in advance.
Answer by tanakatoo · May 05, 2021 at 12:38 PM
Hi @TeamON , I moved all the children from the panel to the canvas and tried it again but it still does the same thing. This is the code I have for the button.
public void Button1()
{
AudioSource.PlayClipAtPoint(soundsRandomized[0].GetAudio().clip, Camera.main.transform.position, 1f);
}
And the code I have to display the canvas is:
private void ShowPopup()
{
if (showPoup == true)
{
//pause the game
Time.timeScale = 0f;
}
else
{
Time.timeScale = 1f;
}
popUp.gameObject.SetActive(showPoup);
In the start function, I have the same code as the button and it works. I created a scriptable object to use for the sounds, so the soundsRandomized list are scriptableobjects that has an audiosource and 2 strings.
I make this popup display when the timer counts down to 0 and when the user clicks on a button, the popup disables again and this repeats throughout the game. If there's another way this should be done, I'm open to changing my code. Thanks in advance.
replace:
popUp.gameObject.SetActive(showPoup);
to:
popUp.GetComponent<Canvas>().enabled = showPoup;
I tried this but it still doesn't work. Funny thing is, when I look at the code attached to the button and checked audiosource.isActiveAndEnabled, it is false. Is it because it is a scriptable object that I attached to the canvas that makes it false? I also tried adding
soundsRandomized[0].GetAudio().enabled = true;
soundsRandomized[0].GetAudio().gameObject.SetActive(true);
before checking the audiosource.isActiveAndEnabled and it is still false. Any ideas? Thanks.
Solved this problem by making an empty game object just to play sounds, following this video in case anyone has the same problem: https://www.youtube.com/watch?v=eWOEyFKqbQg .
Your answer
![](https://koobas.hobune.stream/wayback/20220613033310im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can I play audio clips depending on the players movement 0 Answers