Adding an entry into an array of a class type

Hello!

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;
        }
    }

When assigning values to arrays, you need to provide the index, for example:

audioClips[0] = new AudioClips(audioSource.clip, g);

Currently in your code you’re trying to convert a single object into an array of objects, which it can’t do (thus Cannot implicitly convert …)

I guess what you would like to do here, is to use a list instead of an array.

List<AudioClips> audioClips = new List<AudioClips>();

...

audioClips.Add(new AudioClips(audioSource.clip, g));
1 Like

Thanks!

How would I declare the variable?

I currently have…

 public AudioClips[] audioClips;

However, it isn’t populated when I run this code:

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!");
                    List<AudioClips> audioClips = new List<AudioClips>();
                    audioClips.Add(new AudioClips(audioSource.clip, g));
                }
            }
        }
    }

I’ve tried this, but the variable isn’t visible in the Inspector after the change…

 public List<AudioClips>[] audioClips;

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

1 Like

Thanks, I guess I was wrong about how to set up the variable! It also shows in the inspector now.