Skip to content

Instantly share code, notes, and snippets.

@gkagm2
Last active April 13, 2019 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gkagm2/2bf563255156fadc5a50d04081ffb198 to your computer and use it in GitHub Desktop.
Save gkagm2/2bf563255156fadc5a50d04081ffb198 to your computer and use it in GitHub Desktop.
Unity AudioManager.cs singleton Instance
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioManager : MonoBehaviour {
static AudioManager _instance = null;
public static AudioManager Instance()
{
return _instance;
}
// Use this for initialization
void Start () {
if(_instance == null)
{
_instance = this;
}
}
// Update is called once per frame
void Update () {
}
public void PlaySfx(AudioClip clip)
{
GetComponent<AudioSource>().PlayOneShot(clip);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class csAudioManager : MonoBehaviour {
public AudioClip clip;
public void OnCollisionEnter(Collision collision)
{
AudioManager.Instance().PlaySfx(clip);
Destroy(this.gameObject);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment