Create a separate prefab which includes the door sound. Hold a reference to that prefab on the door. When you destroy the door, also instantiate the door sound prefab at the location of the door. Have a script on that prefab which destroys itself (the instantiated object, not the original prefab), when the sound is done playing.
You can use the same strategy for any number of effects you want to happen when an object is destroyed. I use it often for bullet impact sparks, and the like, for example.
Hey, thanks for the reply but I’m a starter in Unity C# and I’m not really familiar with how exactly I would do the things you just described, I know I’m asking for a lot but could you give examples of the script prefabs
Just writing this off the top of my head, and I haven’t checked it, but it probably works. So this is the script you can make part of the prefab, and also include the audiosource for the door sound. In your existing script, instead of having a reference to its own audiosource and playing it, you have a reference to the prefab and instantiate it. When it is instantiated the script tells it to play, and when the timer runs out it destroys itself.
Instead of a timer you could also just check if the audiosource has stopped playing, but that won’t work if you set it to loop, but a timer usually works fine anyways. Good luck
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoundEffectObject : MonoBehaviour
{
public AudioSource SoundEffect; //AudioSource attached to this prefab
public float TimeUntilDestroy = 2f; //Set in inspector to the amount of time until the object destroys itself
private float timeWhenDestroy;
void Start()
{
timeWhenDestroy = Time.time + TimeUntilDestroy;
SoundEffect.Play();
}
void Update()
{
if (Time.time >= timeWhenDestroy)
{
Destroy(gameObject);
{
}
}