• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by KnightRiderGuy · Oct 27, 2016 at 10:59 PM · c#updatebooleancoroutinesbool

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?

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image elenzil · Oct 27, 2016 at 11:24 PM 0
Share

add a second bool ?

avatar image TreyH · Oct 27, 2016 at 11:59 PM 0
Share

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.

avatar image KnightRiderGuy TreyH · Oct 28, 2016 at 01:25 AM 0
Share

Thanks @treyH, not really sure I follow you, could you explain a little more in depth ?

avatar image TreyH KnightRiderGuy · Oct 28, 2016 at 01:58 AM 0
Share

If you paste your coroutine that starts the audio, I'll show you what I mean.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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();
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KnightRiderGuy · Oct 28, 2016 at 10:58 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

234 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Check bool from another script and use in another script C# 3 Answers

question about array or list of boolean 1 Answer

3d listener position only updates once 0 Answers

What does this mean? 1 Answer

OnTriggerStay not working correctly. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges