using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(AudioSource))] // Ensure AudioSource component is attached public class GrabbableSound : MonoBehaviour { private AudioSource audioSource; private bool isAudioPlaying = false; void Start() { // Get the AudioSource component audioSource = GetComponent(); } private void OnCollisionEnter(Collision collision) { // Play sound clip when collided if audio is not already playing if (!isAudioPlaying && audioSource != null && audioSource.clip != null) { audioSource.PlayOneShot(audioSource.clip); isAudioPlaying = true; } } private void OnTriggerEnter(Collider other) { // Play sound clip when touched if audio is not already playing if (!isAudioPlaying && audioSource != null && audioSource.clip != null) { audioSource.PlayOneShot(audioSource.clip); isAudioPlaying = true; } } // You can add more events as necessary, ensuring to check isAudioPlaying before playing the sound // Optional: Reset isAudioPlaying to false when the audio finishes playing void Update() { if (isAudioPlaying && !audioSource.isPlaying) { isAudioPlaying = false; } } }