Search Unity

Changing an Audio Source's Audio Clip

Discussion in 'Scripting' started by Mirage, Jan 30, 2010.

  1. Mirage

    Mirage

    Joined:
    Jan 13, 2010
    Posts:
    230
    How can I change an Audio Source's Audio Clip via scripting? I have a single audio source and want to cycle it's audio clip without disabling it and enabling another source. I have an array setup but the manual indicates that you can only reference its default audio clip.
     
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
  3. Mirage

    Mirage

    Joined:
    Jan 13, 2010
    Posts:
    230
    It seems that the command AudioClip; only references the default audio clip (on the inspector panel, under audio sorce, Audio Clip). I want to assign different clips to an array of variables and cycle them through the Audio Source. What I want to get at the end is an audio sorce that cycles through 12 audio clips via scripting.

    Here is what I am having problems with, the assigned thingy for the track[#] =

    )The numbers after AudioClip are to indicate different audioclips)
    Code (csharp):
    1.  
    2. track[0] = AudioClip1;
    3. track[0] = AudioClip2;
    4. track[0] = AudioClip3;
    5. track[0] = AudioClip4;
     
  4. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    You say you want to use an array, but your code doesn't reflect that. What you want is something like:

    Code (csharp):
    1. audio.clip = tracks[trackNumber];
     
  5. Sycle

    Sycle

    Joined:
    Nov 24, 2009
    Posts:
    446
    Mirage - AudioSource only holds a single AudioClip, but Jessy linked you to a perfect example of switching it at runtime.

    I'm not sure what you're doing there with your array (I'm assuming assigning them all to element 0 is a typo) although if you're specifying the audio clips via the inspector you can actually use an array and do it more neatly, ie :

    Code (csharp):
    1. var myAudioClip: AudioClip[];
    I threw together a very quick script just to test that the logic worked and ended up with this :

    Code (csharp):
    1. var myAudioClip: AudioClip[];
    2. private var currentClip : int = 0;
    3.  
    4. function Start () {
    5.     LoopClips();
    6. }
    7.  
    8. function LoopClips() {
    9.     while (true) {
    10.         audio.clip = myAudioClip[currentClip];
    11.         audio.Play();
    12.        
    13.         yield WaitForSeconds (audio.clip.length);
    14.        
    15.         currentClip++;
    16.        
    17.         if (currentClip >= myAudioClip.length)
    18.             currentClip = 0;
    19.     }
    20. }
    It uses a coroutine to play the first element of your array, wait till it's done, play the second element and so on, until the end where it will loop.

    Hope that's helpful!
     
  6. Mirage

    Mirage

    Joined:
    Jan 13, 2010
    Posts:
    230
    Thanks Scyle, thats exactialy what I was trying to do. The element 0 was a typo, Copy-paste error :oops: . Thanks for your help.
     
  7. Rispat-Momit

    Rispat-Momit

    Joined:
    Feb 14, 2013
    Posts:
    266
    Hello Scyle! I am trying to create an array of sounds that will play the one after the other, everytime I am using the same UI button in Unity. Can you help? Thanks in advance! Your script above helped me a lot but I still haven't find a solution.