Waiting for audio to finish playing before auto-destructing an object

An event may occur (such as an object pickup or the killing of an enemy) that we wish to notify to the player by playing an audio clip, and an associated visual object (such as an explosion particle system, or a temporary object in the location of the event). However, as soon as the clip has finished playing, we will wish for the visual object to be removed from the scene. This recipe provides a simple way to link the ending of a playing audio clip with the automatic destruction of its containing object.

Getting ready

Try this with any audio clip that is a second or more in duration. We have included the engineSound audio clip inside the 1362_09_04 folder.

How to do it...

To wait for audio to finish playing before destroying a GameObject, follow these steps:

  1. Create an Empty GameObject and rename it to AudioObject. Then, add an Audio Source component to this object (in the Component | Audio | Audio Source menu).
  2. Import the engineSound audio clip and drag it from the Project view to populate the Audio Clip parameter of the Audio Source component of AudioObject, and deselect the component's Play On Awake checkbox:
    How to do it...
  3. Add the following script class to AudioObject:
    using UnityEngine;
    using System.Collections;
    
    public class AudioDestructBehaviour : MonoBehaviour {
      private AudioSource audioSource;
    
      void Start(){
        audioSource = GetComponent<AudioSource>();
      }
    
      private void Update(){
        if( !audioSource.isPlaying )
          Destroy(gameObject);
      }
    }
  4. In Inspector view, disable (un-check) the AudioDestructBehaviour scripted component of AudioObject (when needed, it will be re-enabled via C# code):
    How to do it...
  5. Create a new C# file named ButtonActions, containing the following code:
    using UnityEngine;
    using System.Collections;
    
    public class ButtonActions : MonoBehaviour{
      public AudioSource audioSource;
      public AudioDestructBehaviour audioDestructScriptedObject;
    
      public void PlaySound(){
        if( !audioSource.isPlaying )
          audioSource.Play();
      }
    
      public void DestroyAfterSoundStops(){
        audioDestructScriptedObject.enabled = true;
      }
    }
  6. Create a UI button named PlaySoundButton on the screen with a button Play Sound text, and attach the ButtonActions script to this button.
  7. With PlaySoundButton selected in the Hierarchy, create a new on-click event handler, dragging PlaySoundButton into the Object slot, and selecting the PlaySound() function.
  8. With the PlaySoundButton selected in the Hierarchy panel, drag AudioObject into the Inspector view for the public Audio Source variable AudioObject. Also, drag AudioObject into the Inspector view for the public Script variable AudioDestructScriptedObject, shown as follows:
    How to do it...
  9. Create a second UI button named DestoryWhenSoundFinishedButton on screen, with the button text Destroy When Sound Finished, and attach the ButtonActions script to this button.
  10. With DestoryWhenSoundFinishedButton selected in the Hierarchy panel, create a new on-click event handler, dragging PlaySoundButton into the GO slot, and then selecting the DestroyAfterSoundStops() function.
  11. Just as you did with the other button, now the DestoryWhenSoundFinishedButton selected in the Hierarchy panel, drag AudioObject into the Inspector view for the public Script variable MyAudioDestructObect.

How it works...

The GameObject named AudioObject contains an Audio Source component, which stores and manages the playing of the audio clip. AudioObject also contains a scripted component, which is an instance of the AudioDestructBehaviour class. This script is initially disabled. When enabled, every frame this object (via its Update() method) tests whether the audio source is not playing (!audio.isPlaying). As soon as the audio is found to be not playing, the GameObject is destroyed.

There are two UI buttons created. Button PlaySoundButton calls the PlaySound() method. This method will start playing the audio clip, if it is not already playing.

The second button called DestoryWhenSoundFinishedButton calls the DestoryAfterSoundStops() method. This method enables the scripted component AudioDestructBehaviour in GameObject AudioObject—so that that GameObject will be destroyed, once the sound has finished playing.

See also

  • The Preventing an Audio Clip from restarting if it is already playing recipe in this chapter
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.184.6