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 gimoj · Jul 19, 2014 at 09:49 AM · c#arrayaudioaudiosource

Can't get sound clip from array to play properly

I have an array for 2 audiosources and I'm trying to make the first clip play when it hits the enemy and the second for when my character explodes. The first one works fine but when my character explodes the second one doesn't make a sound. Is there something wrong with my code?

 AudioSource clip1;
 AudioSource clip2;

 void Start () 
 {
     AudioSource[] sounds = GetComponents<AudioSource>();
     clip1 = sounds[0];
     clip2 = sounds[1];

}

 void FixedUpdate ()
 {    

     GroundCheck ();

     if (grounded == false || dragged == true) 
     {
         float h = Input.GetAxis ("Mouse X");
         
         if (h > 0 && !facingRight)
             Flip ();
         else if (h < 0 && facingRight)
             Flip ();
     }

     if (grounded == true) 
     {
         if (walking == true) 
         {
             walker ();
         }
         // Create an array of all the colliders in front of the enemy.
         Collider2D[] frontHits = Physics2D.OverlapPointAll (frontCheck.position);
         
         // Check each of the colliders.
         foreach (Collider2D c in frontHits) {
             // If any of the colliders is an Obstacle...
             if (c.tag == "DirectionTrigger") {
                 // ... Flip the enemy and stop checking the other colliders.
                 Flip ();
                 break;
             }
             if (c.tag == "Enemy") {
                 
                 if (enemyhit == false){
                     if (c.gameObject.GetComponent<Bugbot> ().destructionImminent == false)
                     {
                     // ... Flip the enemy and stop checking the other colliders.
                     animator.SetTrigger("swat");
                     walking = false;
                         clip1.Play();
                     }
                 }
                 break;
             }
         }
     }
     if(HP <= 0 && !dead)
     {
         // ... call the death function.
         Death ();
         StartCoroutine(delayAnim());

     }
 }

 public IEnumerator delayAnim()
 {
     yield return new WaitForSeconds(0.01f);
     animator.SetTrigger ("swatbotexplosion");
     clip2.Play();
 }
Comment
Add comment · Show 4
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 fafase · Jul 19, 2014 at 10:02 AM 1
Share

Do you have a clip in the AudioSource?

First off do you get inside the coroutine? Place a print or a debug to see if the method is reached.

avatar image gimoj · Jul 19, 2014 at 10:11 AM 0
Share

yeap, I just checked now and it goes in. I also have a clip in the audio source but from what I notice is that it's always the first one called that plays.

avatar image gimoj · Jul 19, 2014 at 10:40 AM 0
Share

the second one only plays when i exit the play scene or when the game over scene pops up.

avatar image gjf · Jul 19, 2014 at 10:45 AM 0
Share

does your coroutine get started?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by gimoj · Jul 19, 2014 at 10:55 AM

Nevermind the clips themselves were the problem. I got it to work after changing them. Thanks and sorry for the trouble.

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

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

22 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

Related Questions

Read audio/music currently playing on speakers? 0 Answers

How to start a sound not from it's start ? 2 Answers

[Rhytm game] How to spawn on exact time of music? 3 Answers

Audio is always playing 0 Answers

How to avoid audio glitches whilst switching volume of an Audiosource each Update()-Frame? 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