<aside> 📄 This script will play a sound after a set amount of time.
</aside>
using UnityEngine;
public class PlaySoundAtTime : MonoBehaviour
{
[Tooltip("Time in seconds after scene start to play the sound.")]
public float playTime = 10f;
[Tooltip("Audio clip to play.")]
public AudioClip clip;
private AudioSource audioSource;
private bool hasPlayed = false;
private float timer = 0f;
void Start()
{
audioSource = gameObject.AddComponent<AudioSource>();
audioSource.playOnAwake = false;
}
void Update()
{
if (hasPlayed) return;
timer += Time.deltaTime;
if (timer >= playTime)
{
if (clip != null)
{
audioSource.PlayOneShot(clip);
hasPlayed = true;
}
}
}
}