I'm doing this game where with cards you assign different beats to different channels. You drag and drop the beat to a channel and it gets added to the mixer. What I'm having trouble with is when i try to play each channel individually with an UI Button. This is the script attached to the channels:
public class ManageChannel : MonoBehaviour
{
public AudioSource thisAudioSource;
bool chequeo;
string Nombre;
public AudioMixer audioMixer;
public void AsignaCanal (){
Nombre = this.name; // tomo el nombre del canal actual
thisAudioSource = this.GetComponent<AudioSource>();
var carta = this.transform.GetChild(0); // llamo a la carta hija de este objeto
thisAudioSource.clip = carta.GetComponent<AudioSource>().clip;
thisAudioSource.outputAudioMixerGroup = audioMixer.FindMatchingGroups(Nombre)[0]; // asigno al AudioSource el grupo correspondiente
}
public void comienzaAudio (){
if (!thisAudioSource.isPlaying){
thisAudioSource.Play();
}
else {
thisAudioSource.Stop();
}
}
// Start is called before the first frame update
void Start()
{
chequeo = true;
}
// Update is called once per frame
void Update()
{
if (this.transform.childCount != 0 && chequeo) {
AsignaCanal();
chequeo = false;
thisAudioSource.Play();
}
}
}
What the script does is when a card is dropped, it takes the clip of the card and get its into the AudioSource
of the channel, and that AudioSource
gets outputted to its respective channel (for example, the GameObject
"Channel 1" goes to the mixer group "Channel 1" and so on). Then i've tried to build the buttons to reproduce the track of each channel individually, but using AudioSource.Play()
since I believe I cannot do it with the AudioMixer directly. What i did is create a UIButton and then assign the respective GameObject
Channel to the OnClick()
section on the inspector, and then i choose the following function on the script that i showed before
public void comienzaAudio (){
if (!thisAudioSource.isPlaying){
thisAudioSource.Play();
}
else {
thisAudioSource.Stop();
}
}
Then, every time i press the button, that function should be called. The problem is nothing happens. I'm positive the button is working because i tested with a bool
value and a Debug.Log()
to see if it changes values with every click, and it does. So, i honestly have no clue what the issue is. Any help would be appreciated. Thanks!