- Home /
getting "null" when using array. (javascript)
I've done plenty of arrays in python so I understand the concept behind them, but I've never done one in JavaScript and it is not working. When I Debug.Log a item in the array, it gives me null. My code 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];
whenever I do Debug.Log(number[0])
it gives me null. What am I doing wrong?
Using a ton of separate variables defeats the whole reason for using arrays in the first place. Get rid of all those and just use an array.
Answer by AlucardJay · May 17, 2014 at 08:27 PM
is it an array of AudioClips? Or an array of Strings?
AudioClip :
var number : AudioClips[];
String :
var number : String[] = ["zero","one","two","three","four","five","six","seven","eight","nine"];
If I'm assuming correctly, you are trying to create an array of AudioClips from individual AudioClip components/variables.
You cannot do this outside a function, it would be something like :
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 : AudioClips[];
function Start()
{
number = new AudioClips[10]; // declare the size of the array
// assign values to the array
number[0] = zero;
number[1] = one;
number[2] = two;
number[3] = three;
// etc etc
}
but if you're doing that, why not just drag and drop the AudioClips directly into the array in the inspector?
From your other question : I want to create an array
var number : AudioClips[];
Now look in the Inspector at the variable number. Expand it (click the little grey arrow). Change the size, now you have boxes to drag and drop your clips in.
Your answer
![](https://koobas.hobune.stream/wayback/20220613145254im_/https://answers.unity.com/themes/thub/images/avi.jpg)