using System.Collections; using System.Collections.Generic; using UnityEngine; public class Key : MonoBehaviour { // Get pickup sfx [SerializeField] AudioClip pickupFX; // Components AudioSource audioSource; SpriteRenderer spriteRenderer; // Method runs on first frame void Start() { // Get components audioSource = GetComponent(); spriteRenderer = GetComponent(); } // If key trigger enters anything void OnTriggerEnter2D(Collider2D col) { // If key trigger enters player if (col.gameObject.tag == "Player") { // Play pickup sfx, run waitforfx coroutine and hide key audioSource.PlayOneShot(pickupFX); StartCoroutine(WaitForFX()); spriteRenderer.enabled = false; } } // Coroutine that waits for pickup sfx to play before destroying game object IEnumerator WaitForFX() { yield return new WaitForSeconds(0.8f); Destroy(gameObject); } }