- Home /
Change Bool When Audio Clip Ends C#
I have a function like this:
//Generic Factoids Call Up
public void GenericFactoidCall(){
source.PlayOneShot (FactoidsCallClips [Random.Range (0, FactoidsCallClips.Length)]);
}
The trouble is that I have a bool on another script called "KITT_isSpeaking" that I want to change when the **Audio Clip End*s, I'm currently doing this in Update*:
void Update(){
if (source.isPlaying) {
Debug.Log ("KITT IS TALKING");
KITTmodesObject.KITT_isSpeaking = true;
}
if (!source.isPlaying){
Debug.Log ("KITT IS NOT TALKING");
KITTmodesObject.KITT_isSpeaking = false;
}
}
now this does work for the most part, but the problem I have is that in the other script I have a coroutine that tries to make the: "KITT_isSpeaking" bool True when it starts...... But the update in the Other script changes it back to False.
is there a way for me to have the bool in the coroutine able to change the bool and not have the update in the other script cancels that out?
You could tell that coroutine (that starts it) to yield afterwards until next update, then continue waiting until update while it plays.
At the end, you'd just set that back to false. Controlling the same value from different places is typically clunky, so this would avoid that in your case.
Thanks @treyH, not really sure I follow you, could you explain a little more in depth ?
If you paste your coroutine that starts the audio, I'll show you what I mean.
Answer by kleber-swf · Oct 28, 2016 at 04:33 PM
You can make something like this:
private IEnumerator PlaySound(AudioClip clip, Action onComplete) {
Source.PlayOneShot(clip);
yield return new WaitForSeconds(clip.length);
if (onComplete != null) onComplete();
}
Thanks @kleber-swf, one of the problems I was / Am having is that he has a habit of answering himself when an Audio Clip is Played, the Voice Server document sends a command to a scrip that calls the voice to be played on another game object that I call "VoiceBox" The "VoiceBox" hold many, $$anonymous$$ANY audio clips in $$anonymous$$any different arrays as each voice has multiple responses. So what often happens is that when you say something and then he responds he often mistakes what he is responding with as another voice command.... so I have been trying to come up with some sort of a crafty way around that. One thought was to do sort of what I had in my post question where in update I Check if the audio is playing on the "VoiceBox" Set a "$$anonymous$$ITT_isSpeaking = true" and than back to false when the voice clip ends.... the bool does change so that part works BUT... and this is the tricky part... I think that the time on the bool needs to be somehow "Added onto" so the when I do an If Statement check on the other script that requests the "VoiceBox" responses that it does not for lack of a better word "Hear" the Response as a command...
I tried also in my "VoiceBox" using an invoke method using the Audio Clip Length as a time reference but I got unfavourable results where in many cases on a long Audio Clip that it would NOT set the bool back to false. :/
....lol I'll bet none of that made any sense ;) :/
Your answer
Follow this Question
Related Questions
What does this mean? 1 Answer
OnTriggerStay not working correctly. 0 Answers
Counting points constantly, without framerate dependency 3 Answers
Toggling bools automatically using coroutines 1 Answer
C# Boolean Constantly Changing 1 Answer