Hello, have an index out of range exception, but I don’t know how,
int randomClip=Random.Range(0,clips.Length);
AudioSourcesource=gameObject.AddComponent();
source.clip=clips[randomClip];
thought that since I was using clips.Length where clips is the name of my array, it wouldn’t use a number larger than the size of the array. This is in the awake function, does that matter?
Length is the number of elements in the array, but the index of the array starts at 0, which means it will only go as high as Length-1. Use Length-1 as your upper bound:
int randomClip = Random.Range(0, clips.Length-1);
E.g:
clips[0]
clips[1]
clips[2]
clips[3]
^There are 4 elements in the array, but clips[4] will throw an exception.
I get twice as many debug entries than I expected, but one of them does return that my clip length is 2, and I only have 2 entries which I would assume would be 1.
the array is public and setting it up by typing the size in and dragging and dropping audio files to fill it out.
Strangely, though its on only one object, the debug log upon awake looks like
randomClip = 0
clip.length = 0
IndexoutofRangeException error
randomclip = 1
clip.length = 2
That really looks like it’s on two objects. Is it possible that the object which has it gets duplicated at runtime? Or that a prefab which gets instantiated has it?