using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class YourButtonScript : MonoBehaviour { public AudioSource audioSource; private bool isPlaying = false; void Start() { // Get the AudioSource component from the GameObject audioSource = GetComponent(); } public void PlaySound() { // Check if audioSource and clip are assigned and not already playing if (audioSource != null && audioSource.clip != null && !isPlaying) { // Play the sound clip audioSource.Play(); isPlaying = true; // Invoke a method to reset isPlaying after the clip has finished playing Invoke("ResetIsPlaying", audioSource.clip.length); } else { Debug.LogWarning("AudioSource or AudioClip not assigned or already playing."); } } private void ResetIsPlaying() { isPlaying = false; } }