Playing A Sound in Unity

Here’s a simple way to play a sound in Unity.

1. Add something like this to the declaration section of your script.

public AudioSource audioSourceExplosion;

2. Add this line of code where you want to play the sound.

audioSourceExplosion.Play();

3. NOTE : What I have called audioSourceExplosion here is the name of the variable and of course must match at step one and two.

4. Add an Audio Source component to the GameObject your script is attached to in the Hierarchy.

5. Attach you sound to the new Audio Component.

5. A public variable should have appeared on your GameObject, drag the Audio Source component onto this variable.

Leave a Comment

Your email address will not be published. Required fields are marked *