Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 orchard800 · Sep 28, 2015 at 04:02 AM · audioaudiosourcemultiple

Problems playing multiple Audio Sources in prefabs

Wonder if someone could help me. I'm having an issue plying multiple audio sources on my enemy tank prefabs .

I have added two audio sources (which play just fine elsewhere in the game). I then attach my two audio source components to my script component (below). However, only the first sound will play.

 public AudioSource hitAudioClip;
 public AudioSource deathAudioClip;
 
 Start ()
 {
     AudioSource[] audioSources = GetComponents<AudioSource>();
     hitAudioClip = audioSources[0];
     deathAudioClip = audioSources[1];
 }
 
 TakeDamage()
 {
     hitAudioClip.Play ();
 }
 
 EnemyDeath()
 {
     deathAudioClip.Play ();
 }

I've read a few posts which point at doing it just like this, but I just cant get it to play the second sound. I'm beginning to think it might be some sort of unity bug..?

Am I doing something wrong?

Thanks in advance, Dave

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 amanpu · Sep 28, 2015 at 05:16 AM 1
Share

It should work however it is not a better way to do the same (You should use only one AudioSource component and use some AudioHandler Script to handle which audio is played).
It is not unity bug it should work, you may be doing some wrong like

  • You are using same audio clip for both AudioSources

  • TakeDamage() is getting invoked when actually EnemyDeath() should be invoked
    Let me know if it helps

avatar image orchard800 amanpu · Sep 28, 2015 at 05:30 AM 0
Share

Hi @amanpu,

Thanks for your reply to my question.

I definitely have different audio sources: alt text

And they're both referenced in my script: alt text

Also, EnemyDeath invokes an explosion and then destroys the gameobject (after playing the explosion sound clip, of course) and this is working, so I know EnemyDeath is being called.

I only hear the hit sound though, regardless of their order in the inspector. Note: it does this with any combination of sources, so I know it's not the audio files either.

sources.png (38.4 kB)
script.png (17.5 kB)
avatar image MadDevil · Sep 28, 2015 at 05:21 AM 0
Share

you have already defined the audio sources as public, so just drag drop in inspector and call the "TakeDamage" & "EnemyDeath", accordingly.

There is no need to use it as GetComponent.

avatar image orchard800 MadDevil · Sep 28, 2015 at 05:59 AM 0
Share

This doesn't work either, which lead me to try the GetComponents method.

To be clear, if I remove/comment-out these lines in start():

         //AudioSource[] audioSources = GetComponents<AudioSource>();
         //hitAudioClip = audioSources[0];
         //deathAudioClip = audioSources[1];

I don't get either sound played. Could it be something wrong with Unity 5.2?

avatar image MadDevil orchard800 · Sep 28, 2015 at 06:16 AM 0
Share

public AudioSource hitAudioClip; public AudioSource deathAudioClip;

// void Start () // { // AudioSource[] audioSources = GetComponents(); // hitAudioClip = audioSources[0]; // deathAudioClip = audioSources[1]; // }

 void Update()
 {
     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.T))
     {
         TakeDamage();
     }

     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.E))
     {
         EnemyDeath();
     }
 }

 
 void TakeDamage()
 {

     hitAudioClip.Play ();
 }
 
 void EnemyDeath()
 {
     deathAudioClip.Play ();
 }
Show more comments

1 Reply

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

Answer by MadDevil · Sep 28, 2015 at 06:14 AM

why are you using GetComponet ?

Just drag the audiosource into the public variables, you have defined, assign the clip to individual audiosource and you will be able to play both of them.

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

30 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

Related Questions

How can I play multiple audioclips from the same object? 2 Answers

Multiple audioSources on same gameObject? 1 Answer

How to play sound after another with one button (and after full round continue from where the clip stopped)? 0 Answers

Can I play multiple AudioSources from one gameobject? 8 Answers

Multiple audio samples on one object. 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