// Check if exist another instance of this script void Awake() { if (_instance == null) { _instance = this; DontDestroyOnLoad(gameObject); } }
// Read Audio (this function executes from Unity Audio Thread) void OnAudioFilterRead(float[] data, int channels) { if (!active) { return; } // You can't execute any function of Unity here because this function is working on Unity Audio Thread (this ensure the Metronome Accuracy) // To Fix that you need to execute your function on Main Thread again, don't worry i created an easy way to do that :D // There are so much other fixes to do this, like Ninja Thread. ToMainThread.AssignNewAction().ExecuteOnMainThread(CalculateTicks()); }
// (Fix) Set instance value to null to avoid errors at Assign New Actions if this Object has destroyed void OnDestroy() { _instance = null; }