Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Brandon236 · Mar 01, 2017 at 07:25 AM · audioaudio sourceaudioplay

Having multiple audio sources in a single object?

If I have multiple audio sources in an object then how do I set which sound effect I want to play at a specific time? Here's the script I'm using:

 public AudioSource[] sound;
     public AudioSource sound1;
     public AudioSource sound2;
 
     void Start () {
         sound = GetComponents<AudioSource>();
         sound1 = sound [0];
         sound2 = sound [1];
     }

And to play the sound I use

     sound1.Play ();

The strange part is that no matter how I set the array values, it keeps playing the sound2 audioclip instead of the sound1 one. Even stranger is that if I disable the sound2 audioclip, it doesn't play anything. So what's going on?

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

2 Replies

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

Answer by abdullahsyyd · Mar 01, 2017 at 07:34 AM

This is because the function GetComponents<> always works on top-to-bottom order if you really want to get control on the order of components then you must drag these sources in to inspector rather than getting them on runtime through code. In your case change the order of sound2 this will behave oppositely. So my suggestion is not to use GetComponents<> if you don't need this.

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
avatar image
2

Answer by ExtinctSpecie · Mar 04, 2017 at 09:02 AM

set the audio clips from the inspector where the script is attached to

 public AudioClip audioClip1;
 public AudioClip audioClip2;
 public AudioClip audioClip3;

 AudioSource myAudioSource1;
 AudioSource myAudioSource2;
 AudioSource myAudioSource3;

 void Start()
 {
     myAudioSource1 = AddAudio (true, false, 0.2f);
     myAudioSource2 = AddAudio (false, false, 0.3f);
     myAudioSource3 = AddAudio (true, false, 0.4f);
     StartPlayingSounds ();
 }
 public AudioSource AddAudio(bool loop, bool playAwake, float vol) 
 { 
     AudioSource newAudio = gameObject.AddComponent<AudioSource>();
     //newAudio.clip = clip; 
     newAudio.loop = loop;
     newAudio.playOnAwake = playAwake;
     newAudio.volume = vol; 

     return newAudio; 
 }
 void StartPlayingSounds()
 {
     myAudioSource1.clip = audioClip1;
     myAudioSource1.Play ();

     myAudioSource2.clip = audioClip2;
     myAudioSource2.Play ();
     //and so on
 }
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

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

6 People are following this question.

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

Related Questions

When Audio is played through script it buzzes and glitches 2 Answers

How to play music/audio 2 Answers

How to get this .mp3 conversion script to play through an audio source? 0 Answers

How to create an audio manager script with input from triggers? 2 Answers

Why Music Player Pause and Stop Buttons Do Not Work As They Should 2 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