I am currently working on an audio manager that has an array of sounds and methods to play the sounds and create audio sources for them. I have created the Sound “droppedObject” in the editor on an “Audio Manager” object that contains the “AudioManager” script, however I am unable to reference any of the array. Is there a way to do this in Unity? Just want to be able to pass the sound into CreateAudioSource().
public class AudioManager : MonoBehaviour
{
public Sound[] sounds;
public Sound[] locationSounds;
public void CreateAudioSource(Sound s, GameObject g)
{
if(s.source == null)
{
s.source = g.AddComponent<AudioSource>();
s.source.outputAudioMixerGroup = s.outputMixerGroup;
s.source.clip = s.audioClip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
s.source.loop = s.loop;
}
}
}
public class MainMenuButtons : MonoBehaviour
{
//public Sound droppedObject;
private void Awake()
{
AudioManager audioManager = FindObjectOfType<AudioManager>();
audioManager.CreateAudioSource(droppedObject,gameObject);
}
}