call function with audioclip as variable

Hi All,

I’m trying to make a function that I can call with the name of an audioclip, like this:

SCRIPT #1

var myAudioClip : AudioClip;
function playSound (playObject, soundClip) {
	mitObject = GameObject.Find(playObject);
	mitObject.audio.clip = soundClip;
	mitObject.audio.Play();
}

I then call the function from another script like this:

SCRIPT #2

playSound ("myObject", "myAudioClip");

Trouble is of course that I’m calling the function from script #2 with a string that just contains the name of the variable in script #1, not the actual audioclip.

I tried defining the variable-type like this, which didn’t work:

function playSound (playObject, soundClip:AudioClip)

I also tried replacing this line:

mitObject.audio.clip = soundClip;

with this:

var myTempSoundClip:AudioClip = soundClip;
mitObject.audio.clip = myTempSoundClip;

Which didn’t work either :-/

anyone knows the answer to this?? - I really need to figure it out…

i’m probably not the person to be answering this, but i’ll take a shot:

1-do you only have one audio clip as shown? if so then just use audio.play ()

2-if not, i would just keep all your audio sources in an array and have the second argument of your playSound function be an int so you call it as “PlaySound (playObject, 1)” then pull that from the array.

your other code seems a bit confusing to me, whats a playObject?

Thanks for your reply

  1. No, I have several audio clips

  2. Haven’t thought about using an array before, but won’t I have the same problem that the array only contains a string and not the actual audioclip?

you can have an array of anything, sound clips, transforms etc. - like so:

var myAudioClips : AudioClip[ ];

then you just drag your clips into their slots in the inspector.

could you clarify what you’re setup is? why exactly are you using strings?

I’m using strings because I have a pretty complicated dialogue-system, so I need to call my function with a string that’s following a naming convention.

F.ex. there’s a person in the game called Mamba, so I call my function with a string like this: “person_mamba_s1_q1_audio”. The string not only contains the name of the audioclip I want to play, but also what section and type of dialogue I need to play.

Is there any way to name the audioclips inside my array, so I don’t have to refer to them by number? Like this:

print(completeSoundArray["person_mamba_s1_q1_audio]");

Here’s an idea for you.

Make two arrays, one holding the sound clips (SoundClips[ ]), and the other holding the labels(SoundClipLabels[ ]). Each sound entry should have the same index as its corresponding sound clip.

Make a function FindSoundClip() which takes a string as its argument. It should return an integer which is the index of the clip that you want. It should look something like this (note that I have NOT tested this, so it’s probably riddled with typos!):

function FindSoundClip(name : String) : int {
   for(L : int = 0; L < SoundClipLabels.length; L++) {
      if (SoundClipLabels[L] == name) return L;
   }
   Debug.Log("Clip not found: " + name);
   return -1;
}

Next, make a function PlaySoundClip() that takes an integer, and plays that particular sound clip from your array of sound clips:

function PlaySoundClip (index : int) {
   Clip : AudioClip = SoundClips[index];
   AudioObject.audio.clip = Clip;
   AudioObject.audio.Play();
}

Finally, for convenience, create one more function PlayClipByName() that takes a string as its argument and calls the other two functions like so:

function PlayClipByName(name : String) {
   PlaySoundClip(FindSoundClip(name));
}

If you’re working in mono, it becomes extra-easy, because then you can just create a struct and use one array:

struct ClipEntry {
   string Name;
   AudioClip Clip;
}

public ClipEntry[] ClipDatabase;

Finally, create only one function for finding the clip:

public AudioClip FindSoundClip(string name) {
   for (int L = 0; L < ClipDatabase.Length; L++) {
      if (ClipDatabase[L].Name == name) 
      return ClipDatabase[L].Clip; 
   }
   Debug.Log("Clip not found: " + name);
   return null;
}

And your convenience function:

public void PlayClip(string name) {
   AudioObject.audio.clip = FindSoundClip(name);
   AudioObject.audio.Play();
}

Make sure to add an object variable at the top of your code:

// Javascript:
   var AudioObject : GameObject;

// Mono:
   public GameObject AudioObject;

Drag (or programmatically assign) the object that you want to play the sound, to the slot in the inspector that’s labelled AudioObject. This will be on whatever object you placed the script on. Make sure that whatever arrays you create are globally accessible, e.g., outside of all functions in Javascript, and inside of only the class declaration in Mono.

Doing it this way, you can make your label whatever you want, and not be restricted to the filename. Like “Billy trying to imitate Jabba the Hutt” instead of “billy_jabba_imitate.wav” or whatever.

Be careful with this script, because I’m missing some sleep and I might have goofed on something or another. Make sure to proofread it. :slight_smile:

Hope that at least gives you a starting point!