Search Unity

Multiple GvrAudioSources on an object?

Discussion in 'Daydream' started by pfreema1, Apr 14, 2017.

  1. pfreema1

    pfreema1

    Joined:
    Apr 5, 2017
    Posts:
    21
    Hi!

    I have a gameObject that is an audio source that emits three different sounds. I tried adding 3 different Gvr Audio Source components and made a script that plays them using this way to play the sound:
    Code (CSharp):
    1. teleporter.GetComponent<GvrAudioSource>().Play("WarpDrive_02");
    This wasn't working though. How do you play different sound clips for the same object?
     
  2. anokta

    anokta

    Joined:
    Aug 12, 2012
    Posts:
    6
    Hi,

    I would recommend using PlayOneShot function to achieve that, here's a very simple example:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MultiSoundPlayer : MonoBehaviour {
    4.   // Audio clips of different sounds.
    5.   public AudioClip[] clips;
    6.  
    7.   // Audio source.
    8.   public GvrAudioSource source;
    9.  
    10.   // Plays the sound with given |index| and |volume|.
    11.   public void Play(int index, float volume) {
    12.     source.PlayOneShot(clips[index], volume);
    13.   }
    14. }
    Otherwise, if you wish to play - loop - all the sounds simultaneously, you can split the audio sources to their delegated game objects in scene hirearchy, i.e., by adding a GameObject per each GvrAudioSource component as a child of your main teleporter game object, in order to make sure they don't clash with each other.

    Hope it helps!
     
  3. pfreema1

    pfreema1

    Joined:
    Apr 5, 2017
    Posts:
    21
    That works perfect! I'll definitely be saving that code snippet for the future. Thank you!