How it works...
You created a ButtonActions script class and added an instance as a component to the Main Camera GameObject. This has two public variables, one to an AudioSource and one to an instance of the AudioDestructBehaviour scripted component.
The GameObject named AudioObject contains an AudioSource 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 in this object (via its Update() method) tests whether the audio source is playing (!audio.isPlaying). As soon as the audio is found to be not playing, the GameObject is destroyed.
There are two UI buttons ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access