- Home /
Question by
glasshalfpool · Feb 04, 2021 at 10:40 PM ·
audioplayoneshot
Creating a PlayOneShot function in an audio manager
I followed a Brackeys tutorial to create an audio manager which included a Play function. I managed to create a second function for stop which works fine, but have not have any luck creating one for PlayOneShot. Anyone able to suggest how to write this:
public class AudioManager : MonoBehaviour
{
public Sound[] sounds;
// Start is called before the first frame update
void Awake()
{
foreach (Sound s in sounds)
{
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
s.source.loop = s.loop;
}
}
public void Play(string name)
{
Sound s = Array.Find(sounds, sound => sound.name == name);
s.source.Play();
}
public void PlayOnce(string name, float volume)
{
Sound s = Array.Find(sounds, sound => sound.name == name);
s.source.PlayOneShot();
}
public void Stop(string name)
{
Sound s = Array.Find(sounds, sound => sound.name == name);
s.source.Stop();
}
}
Comment