/// <summary> /// Adds a new Audiosource the SoundManager and sets it as a child object of the emitter. /// When the sound isn't looping, the Audiosource will be destroyd after it is played. /// </summary> /// <param name="clip">Audioclip which should be played.</param> /// <param name="emitter">Parent object of the AudioSource.</param> /// <param name="volume">Volume of the clip.</param> /// <param name="pitch">Pitch of the clip.</param> /// <param name="loop">Specifies if the clip should be looped or not.</param> public AudioSource Play(AudioClip clip, Transform emitter, float volume, float pitch, bool loop, AudioGroup audioGroup) { //Instance a new GameObject GameObject g = new GameObject("AudioSource: " + clip.name); g.transform.position = emitter.transform.position; g.transform.parent = emitter; //AudioSource AudioSource source; source = g.AddComponent <AudioSource>(); source.outputAudioMixerGroup = audioMixer.FindMatchingGroups(audioGroup.ToString())[0]; source.clip = clip; source.volume = volume; source.pitch = pitch; source.loop = loop; source.Play(); audioSources.Add(source); if (!loop) { //Destroy(g, clip.length); StartCoroutine(WaitForDestroy(g, clip.length)); audioSources.Remove(source); } return(source); }