- Home /
Duplicate Question : http://answers.unity3d.com/questions/709627/getting-null-when-using-array-javascript.html
How to create an auido array (JavaScript)
I am creating a program where it reads out a certain number. To to this, I have an audio file of numbers 0 - 9. I want to create an array where I can say
audio.clip = number[4];
audio.Play();
And have this play the sound file 'four'. What I have currently is
var zero: AudioClip;
var one: AudioClip;
var two: AudioClip;
var three: AudioClip;
var four: AudioClip;
var five: AudioClip;
var six: AudioClip;
var seven: AudioClip;
var eight: AudioClip;
var nine: AudioClip;
var number = [zero,one,two,three,four,five,six,seven,eight,nine];
Answer by screenname_taken · May 24, 2014 at 09:33 PM
just declare var myaudio:AudioClip[];
then back in the editor, you'll see a "Sound Options" section in your object with that script. Now you just type in the number 10 in the "size" box and you'll get 10 places for your sounds. Then in your scripts just call audio.clip=myaudio.[0] (The first number in the array is 0 and will go up to 9 for a 10-slot array.
The first part is right.
Use :
var myaudio : AudioClip[];
then look in the Inspector at the variable myAudio. Expand it (click the little grey arrow). Change the size, now you have boxes to drag and drop your clips in.
This is the same as your last question (basically).