- Home /
Question by
Trendsetter123 · Oct 07, 2019 at 02:04 PM ·
audiobooleantoggle button
How do i toggle audio off and on?
here is my code so far, It plays the audio, but I cannot figure out why the audio will not stop, I tried everything, even update methods. Can someone help me? Heres the code
public class OrchestralSwitch : MonoBehaviour
{
//Play the music
bool m_Play;
//Detect when you use the toggle, ensures music isn’t played multiple times
bool m_ToggleChange;
public AudioClip a;
public AudioSource audio;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
public void OnCLick()
{//Play the audio you attach to the AudioSource component
AudioSource audio = gameObject.AddComponent<AudioSource>();
//Load AudioMixer
AudioMixer audioMixer = Resources.Load<AudioMixer>("mIXER");
//Find AudioMixerGroup you want to load
AudioMixerGroup[] audioMixGroup = audioMixer.FindMatchingGroups("Bass");
//Assign the AudioMixerGroup to AudioSource (Use first index)
audio.outputAudioMixerGroup = audioMixGroup[0];
bool m_Play = gameObject.GetComponent<Toggle>().isOn;
//Check to see if you just set the toggle to positive
if (m_Play)
{
audio.clip = a;
audio.Play();
}
else
{
if (m_Play == false)
{
audio.Stop();
//Ensure audio doesn’t play more than once
}
}
}
void Update()
{
//....
if (!audio.isPlaying)
{
if (m_Play == false)
{
audio.Stop();
}
}
}
}`
I have inserted the proper audioclip, audiosource, and variables in the top so no worries, I just can't figure it out. Any help would help, thanks
Comment