Learn to code by making a 2D space shooter game.
Objective: Implement Powerup sound in another way.
The first step selects all powerups to add an audio source component.
The next step is to go through the powerup scripts, create an audio source variable and cache it to the start method.
The next step is to drag a powerup sound clip and drag it to both powerup prefabs in the project folder.
If we call audio source. Play it won't work, because all code will be run in the same frame because destroy method will destroy powerup object we won't hear the audio.
To solve this issue let's create a sprite render variable and assign the component in the start method.
Let us disable the sprite render then Play the powerup sound to give the illusion that we collect it .
Last let us delay the destroy the game object for about 3 seconds to make sure the sound clip is played to the end.
This is another way to play sound simultaneous while giving the player the illusion that he successfully collect the powerup.
See you in the next article.