Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Benjeta · Jun 29, 2014 at 05:31 PM · audiowaitsource

Wait for clip finish and activate other audio source

Hi, I've a little question.

I want to play a sound and desactivate ALL others audio sources if the player pick some stuffs. In my script bellow, the sound stop and the new play very well. But I'm unable to reactivate all the others audio sources AFTER the clip has finished...

Here my C# script :

 public class Sign : MonoBehaviour {
     private bool revertFogState = false;
     public Font f;
     public int SignsFound = 0;
     public AudioClip pickSound;
     public AudioClip DANCE;
     public Transform explosion;
     public Transform win;
     private GameObject toDestroy;
     private bool destroyObjectSwitch;
     private bool pickup;
     private bool collided;
     private int tempo;        
 
     void Start () {
         this.destroyObjectSwitch = false;
         this.pickup = false;
         this.collided = false;
     }
 
     void Update () {
         if(Input.GetButton("Fire1")){
             this.pickup = true;
         }else{
             this.pickup = false;    
         }    
         Debug.Log ("pickup : "+this.pickup+" Collided : "+this.collided);
         if (Input.GetButtonDown("Pickup") && this.collided == true){
             AudioSource.PlayClipAtPoint(pickSound, transform.position );
             Instantiate(explosion, transform.position, transform.rotation);
             Destroy(this.toDestroy.gameObject);
             this.SignsFound++;   
             this.pickup = false;
             this.collided = false;
             this.tempo = 100;
             if (SignsFound == 1){
                 AudioSource[] audios = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
                 foreach(AudioSource aud in audios)
                 aud.volume = 0;            
                 AudioSource.PlayClipAtPoint(DANCE, transform.position );
                 Instantiate(win);
                 yield return new WaitForSeconds(audio.clip.length);
                 AudioSource[] audios = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
                 foreach(AudioSource aud in audios)
                 aud.volume = 0;
                 RenderSettings.fog = revertFogState;     
            }                
        }
        if(this.tempo>0)this.tempo--;        
     }
       
     void OnTriggerEnter(Collider other){
         Debug.Log (other.gameObject.name);
         if(other.gameObject.tag == "Sign" ){
             this.collided = true;
             this.toDestroy = other.gameObject;
         }    
     }
     
     void OnTriggerExit(Collider other){
         this.collided = false;
         this.toDestroy = null;
     }
     
     void OnGUI(){
         if(this.tempo>0)
             GUI.Label (new Rect (Screen.width/2-100,Screen.height/2-10,500,500), "Tu as "+this.SignsFound+" panneau (x) SUR 5.");
             GUI.skin.font = f;
     }
 }

I tried to make a Yield returne new for the clip, but I cant reactivate the others sounds. Can you help me ? ^^

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by fafase · Jun 29, 2014 at 05:41 PM

Your problem is that you are trying to yield from an update method and that is not possible.

You need to create a coroutine to start.

 if (SignsFound == 1){
    StartCoroutine(PlaySound());
 }


 IEnumerator PlaySound(){
     AudioSource[] audios = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
         foreach(AudioSource aud in audios)
             aud.volume = 0;
     AudioSource.PlayClipAtPoint(DANCE, transform.position );
     Instantiate(win);
     yield return new WaitForSeconds(audio.clip.length);
     // No need for this line below you already have the array
     // AudioSource[] audios = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
     foreach(AudioSource aud in audios)
         aud.volume = 1;  // Here I think you meant 1 to set them back to normal
     RenderSettings.fog = revertFogState;
 }

 
Comment
Add comment · Show 3 · 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 Benjeta · Jun 29, 2014 at 06:33 PM 0
Share

Thanks ! But I've got an error !

Unexpected symbol : line 52

Can you post the entire script to check if I'm doing something wrong please ? ^^

avatar image fafase · Jun 29, 2014 at 06:59 PM 0
Share

The first part goes in the Update, the second part goes in the class as a method.

avatar image Benjeta · Jun 29, 2014 at 08:03 PM 0
Share

Sorry, I don't understand :/ EDIT : I figured out ! Thanks ! ;)

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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Inconsistent ReverbZone / AudioSource.volume behavior 2 Answers

Getting Unity to listen to desktop audio 2 Answers

c# and game objects active state issues 1 Answer

Volume rolloff: Distance to listener not changing. 3 Answers

AudioSource from mesh instead of one point 1 Answer


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