Skip to main content
Search our knowledge base

How do I play multiple Audio Sources from one GameObject?

Comments

8 comments

  • Dumitrache Catalin

    Cool , thank you !

    0
  • Deeds

    How and why does this work?

     

    What are the costs of doing this?

     

    And why do you cache a reference (seemingly unnecessarily) to the AudioSource?

    2
  • Shanice Alexis

    @Deeds To answer your last question: you use GetComponents<AudioSource> to retrieve all AudioSource instances, from which you can then extract the relevant audio clips. Not sure why this isn't shown in the code example though. You might find this more helpful: https://answers.unity.com/questions/175995/can-i-play-multiple-audiosources-from-one-gameobje.html

    -2
  • Deeds

    Sorry, I still don't see how the reference is being used. 

     

    Nor how this works. 

     

    Is the code example completely wrong? It seems like it is.

     

     

    2
  • Shanice Alexis

    It's outdated. You'd maybe do something like this:

     

    7
  • Deeds

    argh. THANK YOU!

     

    This is almost exactly what I'm doing... and I kind of felt like it was overkill to have an AudioSource for every single AudioClip, so went hunting for ways to play multiple clips at the same time, through one AudioSource.

    Which lead me to here, and confusion.

    Seems there really is just a one:one ratio between AudioSources and AudioClips.

     

    Again. Thank you. Your way seems better than mine... an ARRAY! I'll try it.

     

    0
  • mrochefort12v

    The code example is either wrong or outdated because according to the Scripting API, .PlayOneShot() is called on an AudioSource not an AudioClip.

    https://docs.unity3d.com/ScriptReference/AudioSource.PlayOneShot.html?_ga=2.233915998.1929434097.1569861847-1592633089.1569617611 

     

    0
  • Brona Martin

    Hello,

    I want one empty game object to trigger 8 separate audio sources which are in a circle around the player. How do I do this? I am not very good at programming. I want the player to walk into the middle of room and then trigger the sounds. And then when they leave the sound stops.

    Many thanks!

    0

Article is closed for comments.