- Home /
AudioSource wont play even when calling ButtonCloseSFX.Play();
Hello unity developers! I'm a junior game dev and I've encountered an issue while trying to get an AudioSource to play on a button click.
public AudioSource ButtonCloseSFX;
public void Close()
{
SummaryOpen = false;
ButtonCloseSFX.Play();
SummaryMenuObject.SetActive(false);
}
When the button is clicked, the audio source won't play, I've tried testing it with PlayOnAwake and the audio does play. Just when called via script it doesn't play.
I'm running 2019.4.35f, any help would be appreciated.
Answer by VioletAbstract · Mar 05 at 01:58 AM
I'm assuming you have the AudioSource component on SummaryMenuObject, so you are playing the audio then immediately disabling the SummaryMenuObject on the next line. Audio can only play when an object is active. Try creating an AudioManager script and GameObject that will always stay active in the scene, then get a reference to that object in your script and play the audio source from there.