- Home /
Array of audio clip arrays?
I have an a few arrays of audio clips. Can I put all of those arrays into one big array? I know you can have several arrays of integers make up one larger array, but I am not quite how to put one type of array into another...Thanks
Unity does not support serialized multi-dimensional or jagged arrays. Unless you write your own classes that manage this kind of thing, I'm afraid you're out of luck. Otherwise, do a search on this site for multidimensional arrays, and I'm sure you'll find something you can use.
This is not the same thing as arrays, but you could put all of your arrays into a class.
Answer by fherbst · Mar 15, 2012 at 08:57 AM
As syclamoth pointed out, Unity can't serialize (save) moredimensional arrays. The workaround is putting your "inner" array into a class and have an array of this class.
Copy the following code into a new script "MyAudioClipHolder.cs" and see for yourself how this behaves in the editor:
using UnityEngine;
using System.Collections;
// System.Serializable lets you change this class in editor
[System.Serializable]
public class AudioClipArray {
// your "inner" array
public AudioClip[] clips;
}
public class MyAudioClipHolder : MonoBehaviour {
// the "outer" array
public AudioClipArray[] clipArrays;
}
Answer by ow3n · Jul 04, 2017 at 04:49 PM
I realize I am resurrecting an ancient post but after a long search I want to acknowledge how helpful the answer by @felix. was; as well as to share how I implemented it. If you use it wrong you will get the following error:
error CS0021: Cannot apply indexing with [] to an expression of type `AudioClipArray'
So, in a SoundController.cs file...
using UnityEngine;
using System.Collections;
// System.Serializable lets you change this class in editor
[System.Serializable]
public class AudioClipArray {
// your "inner" array
public AudioClip[] clips;
}
public class SoundController : MonoBehaviour {
// the audio component on the GameObject this script is attached to
private AudioSource audio;
// multi-dimensional array of all the sounds
public AudioClipArray[] clipArrays;
void Start(){
// play the sound (note the ".clips" reference there)
audio.PlayOneShot (sounds[int1].clips[int2], .5);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613065138im_/https://answers.unity.com/themes/thub/images/avi.jpg)