How to do it...

To schedule a sound to play after a given delay, do the following:

  1. Create a new Unity 2D project and import the provided sound clip files.
  2. Create a GameObject in the scene containing an AudioSource component linked to the metronome_tick AudioClip. This can be done in a single step by dragging the music clip from the Project panel into either the Hierarchy or Scene panels.
  3. Repeat the previous step to create another GameObject, containing an AudioSource linked to the metronome_tick_accent clip.
  1. For both AudioSources created, uncheck the Play Awake property  so these sounds do not begin playing as soon as the scene is loaded.
  2. Create an empty GameObject named MetronomeManager.
  3. Create a C# script class, Metronome, in a new folder, _Scripts, containing the following code, and add an instance as a scripted component to the MetronomeManager GameObject:
using UnityEngine; 
 
public class Metronome : MonoBehaviour { 
    public AudioSource audioSourceTickBasic; 
    public AudioSource audioSourceTickAccent; 
 
    public double bpm = 140.0F; 
    public int beatsPerMeasure = 4; 
 
    private double nextTickTime = 0.0F; 
    private int beatCount; 
    private double beatDuration; 
 
    void Start() { 
        beatDuration = 60.0F / bpm; 
        beatCount = beatsPerMeasure; // so about to do a beat 
        double startTick = AudioSettings.dspTime; 
        nextTickTime = startTick; 
    } 
 
    void Update() { 
        if (IsNearlyTimeForNextTick()) 
            BeatAction(); 
    } 
 
    private bool IsNearlyTimeForNextTick() { 
        float lookAhead = 0.1F; 
        if ((AudioSettings.dspTime + lookAhead) >= nextTickTime) 
            return true; 
        else 
            return false; 
    } 
 
    private void BeatAction() { 
        beatCount++; 
        string accentMessage = ""; 
 
        if (beatCount > beatsPerMeasure) 
            accentMessage = AccentBeatAction(); 
        else 
            audioSourceTickBasic.PlayScheduled(nextTickTime); 
 
        nextTickTime += beatDuration; 
        print("Tick: " + beatCount + "/" + signatureHi + accentMessage); 
    } 
 
    private string AccentBeatAction() { 
        audioSourceTickAccent.PlayScheduled(nextTickTime); 
        beatCount = 1; 
        return " -- ACCENT ---"; 
    } 
} 
  1. Select the MetronomeManager GameObject in the Hierarchy. Drag metronome_tick from the Hierarchy into the Inspector Audio Source Tick Basic public variable, for the Metronome (Script) component.
  2. Drag metronome_tick_accent from the Hierarchy into the Inspector Audio Source Tick Accent public variable, for the Metronome (Script) component:
  1. Play the scene. You will see (in the Console) and hear the regular metronome sounds, with the first beat of each count playing the accented (and louder) sound.
  1. Try changing the Bpm (beats per minute) setting, to speed up or slow down the metronome. Or change the number of Beats Per Measure, to count up to 3, 4, or 6 beats between each accented beat.