Skip to content

Instantly share code, notes, and snippets.

@kojinkai
Created December 30, 2018 17:24
Show Gist options
  • Save kojinkai/4cf6dfdaf1dc96cca1cd428df12642f8 to your computer and use it in GitHub Desktop.
Save kojinkai/4cf6dfdaf1dc96cca1cd428df12642f8 to your computer and use it in GitHub Desktop.
using UnityEngine;
public class SoundTrackController : MonoBehaviour
{
private void Awake()
{
// We want this soundTrackController to persist across every scene (singleton)
// so we check if there is an existing instance and reuse that, destroying the attempted new
// instance in the process.
// GetType returns the type of the current gameobject the script is attached to
if (FindObjectsOfType(GetType()).Length > 1)
{
gameObject.SetActive(false);
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment