To wait for audio to finish playing before destroying its parent GameObject, perform the following steps:
- Create a new Unity 2D project and import the sound clip file.
- Create a GameObject in the scene containing an AudioSource component that is linked to the engineSound AudioClip. This can be done in a single step by dragging the music clip from the Project panel into either the Hierarchy panel or Scene the panel. Rename this as the AudioObject GameObject.
- Uncheck the Play Awake property for the AudioSource component of the engineSound GameObject. This is to ensure the sound does not begin playing as soon as the scene has been loaded.
- Create a C# script class, called AudioDestructBehaviour, in a new folder, _Scripts, that contains the following code. Also, add an instance as a scripted component to the AudioObject GameObject:
using UnityEngine; using UnityEngine; public class AudioDestructBehaviour : MonoBehaviour...