AudioClip[] Array, initialization & audio sources

I am trying to create my own audio manager script that attachs to the camera with 3 audio sources defining the input so I can control each ones volume with NGUI sliders this works just fine and allows me keep volume global settings as below.

    public float MasterVol = 1;
    public float MusicVol = 1;
    public float SFXVol = 1;
    public float VoiceVol = 1;

What I have got stuck with is I have 3x AudioClip[ ] arrays that I wish to drag and drop the files onto to fill it

How do I initalize the array if I drag and drop the audio files to the array they are set out as follows.

    public AudioSource Music;
    public AudioSource SFX;
    public AudioSource Voice;
    public AudioClip[] MClips; // Audio store for all music clips
    public AudioClip[] SClips; // Audio store for all SFX Clips
    public AudioClip[] VClips; // Audio Store for all Voice Clips

I also need to then call a function for each of the 3 sources and assign the array clips to one of each so I can keep the sources seperate any ideas and insight into this would be great.

I will post the full script below

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class AudioManager : MonoBehaviour
{
    public AudioSource Music;
    public AudioSource SFX;
    public AudioSource Voice;

    public float MasterVol = 1;
    public float MusicVol = 1;
    public float SFXVol = 1;
    public float VoiceVol = 1;

    public int MIndex = 0;
    public int SIndex = 0;
    public int VIndex = 0;

    public AudioClip[] MClips;
    public AudioClip[] SClips;
    public AudioClip[] VClips;

    private AudioManager instance;

    void awake()
    {
        if (instance != null && instance != this)
        {
            Destroy (this.gameObject);
            return;
        }

        else
        {
            instance = this;
        }
        //DontDestroyOnLoad (this.gameObject);
    }

    public AudioManager GetInstance()
    {
        return instance;
    }

    void start()
    {

    }

    public void MasterVolumeChange (float MasterVolume)
    {
        MasterVol = MasterVolume;
        AudioListener.volume = MasterVol;
    }
   
    public void MusicVolumeChange (float MusicVolume)
    {
        if (MasterVol <= 1)
        {
            MusicVol = MusicVolume;
            Music.ignoreListenerVolume = false;
            Music.volume = MusicVol;
        }

        else
        {
            MusicVol = MusicVolume;
            Music.ignoreListenerVolume = true;
            Music.volume = MusicVol;
        }
    }

    public void SFXVolumeChange (float SFXVolume)
    {
        if (MasterVol <= 1)
        {
            SFXVol = SFXVolume;
            SFX.ignoreListenerVolume = false;
            SFX.volume = SFXVol;
        }

        else
        {
            SFXVol = SFXVolume;
            SFX.ignoreListenerVolume = true;
            SFX.volume = SFXVol;
        }
    }

    public void VoiceVolumeChange (float VoiceVolume)
    {
        if (MasterVol <= 1)
        {
            VoiceVol = VoiceVolume;
            Voice.ignoreListenerVolume = false;
            Voice.volume = VoiceVol;

        }

        else
        {
            VoiceVol = VoiceVolume;
            Voice.ignoreListenerVolume = true;
            Voice.volume = VoiceVol;
        }
    }
}

You don’t have to, the serialization system will do it for you. During a Unity Object’s instantiation any serialized data provided for that object is used to populate its public or serializable fields. This means that if you mark something as public or [SerializeField] and give it data in the Inspector, your code can access that data as if you’d initialized it yourself.

Thank you for the tip, I have another question, I have created a function, is it possible to send a parameter or more so how I can edit it so it will accept a parameter for instance I have 5 audio tracks in my array I want this function to be universal to play all music tracks but I only want to play number 3 from an external script? I hope that makes sense or I am clear enough

    public void PlayMusic()
    {
        if (Music != null && MClips !=null)
        {
            if (MasterVol <= 1)
            {
                Music.clip = MClips [MIndex];
                Music.ignoreListenerVolume = false;
                Music.PlayOneShot(MClips[MIndex], MusicVol);
            }
      

            else
            {
                Music.clip = MClips [MIndex];
                Music.ignoreListenerVolume = true;
                Music.PlayOneShot(MClips[MIndex], MusicVol);
            }
        }
    }

You could do something like this (if I’ve understood you correctly):

public void PlayMusic()
{
    PlayMusic(MIndex);
}

public void PlayMusic(int useIndex)
{
    if (Music != null && MClips !=null)
    {
        if (MasterVol <= 1)
        {
            Music.clip = MClips [useIndex];
            Music.ignoreListenerVolume = false;
            Music.PlayOneShot(MClips[useIndex], MusicVol);
        }
        else
        {
            Music.clip = MClips [useIndex];
            Music.ignoreListenerVolume = true;
            Music.PlayOneShot(MClips[useIndex], MusicVol);
        }
    }
}

This will default to using MIndex when you call it without any argument, but playing with the index given if you give one.

This seems like it what I require I shall test it out and will post back if any problems as I am not at my usual pc with all the source code on :slight_smile:

I have either misunderstood something or I am doing things wrong but to double check I am still unsure how I am to select a index number for int useIndex I have tried to pass a variable through it with sendmessage for testing and no music is played at all

I have worked with lists before but I seem unable still to be able to pass the int variable over so only that one audio clip is played through the function, it does work however I do not use any variable and just PlayMusic tied to a button and using the first function you posted it will play only the first music clip every time it’s pressed, if I use the second int which is the one I wish to parse the index number on is the one that currently doesn’t work for me as I am unsure how to parse the index over from another script it seems.

If I use the PlayMusic on a button without the second field with an int in it I get an error which I had expected just very confused right now with what I am doing wrong.

clarity recap, I wish to use the PlayMusic(int useIndex) but unsure how I am to send the int useIndex for example track 3 from another script / game object so the Main Musicplay function will play it from the drag & dropped on Audio clip array, or how to access the MIndex from another script if I am doing it correct

Target.SendMessage(PlayMusic, which int goes here??, SendMessageOptions.Don'tRequireReceiver);
AudioManager.GetInstance().PlayMusic( 2 );

Thank you for the help, I also got it to work by using the code below and then use SomeScript.PlayMusic(1); etc which is a huge step for me to work out how to get this audio system coded to how I would like it :slight_smile:

Edit And whoops I needed to make the Audiomanager instance static but all working now :slight_smile:

public AudioManager SomeScript;

Glad I could help, good to see you got it working :slight_smile: