I’m somewhat not the best at coding. The code below, I have a class called AudioClips. I’d like to be able to add an entry to the audioClips[ ] variable, with the line audioClips = new AudioClips(audioSource.clip, g);, but I’m not sure how to go about it.
The error is: Cannot implicitly convert type SFB_AudioSnapshotExporter.AudioClips' to SFB_AudioSnapshotExporter.AudioClips[ ]’
Any idea where I’m going wrong? Thanks!!!
public void LoadAudioClips(){
foreach (Transform child in transform)
{
Debug.Log ("Child: " + child.gameObject.name);
AudioSource audioSource = child.gameObject.GetComponent<AudioSource>();
AudioMixerGroup audioGroup = audioSource.outputAudioMixerGroup;
Debug.Log ("Group: " + audioGroup.name);
for (int g = 0; g < audioGroups.Length; g++) {
if (audioGroups [g].name == audioGroup.name) {
audioClips = new AudioClips(audioSource.clip, g);
}
}
}
}
[System.Serializable]
public class AudioClips
{
public AudioClip clip;
public int groupID;
public AudioClips(AudioClip newClip, int newGroup)
{
clip = newClip;
groupID = newGroup;
}
}
Inside the if-statement you’re redefining “audioClips”. So “audioClips” no longer refers to your array, but to the list you just created. When the list goes out of scope (when you close the if-statement with the curly bracket) and there are no other references to it, it cannot be accessed anymore. Thus, you’re creating a new list every time and throwing it away.
Also, in your last code sample the variable would be an array of lists.
Here’s what you could do:
// create a list of audioclips
public List<AudioClips> audioClips = new List<AudioClips>();
public void LoadAudioClips(){
foreach (Transform child in transform)
{
Debug.Log ("Child: " + child.gameObject.name);
AudioSource audioSource = child.gameObject.GetComponent<AudioSource>();
AudioMixerGroup audioGroup = audioSource.outputAudioMixerGroup;
Debug.Log ("Group: " + audioGroup.name);
for (int g = 0; g < audioGroups.Length; g++) {
if (audioGroups [g].name == audioGroup.name) {
Debug.Log ("Found a Match!");
// no redefining of "audioClips", so we're using the list at the top
audioClips.Add(new AudioClips(audioSource.clip, g));
}
}
}
}
Check this site for some info on lists:
EDIT: The variable is not visible in the inspector, since I doubt Unity supports serialization of an array of lists. Unity - Manual: Script serialization