0
0
UnityHow-ToBeginner ยท 3 min read

How to Play Sound on Event in Unity: Simple Guide

In Unity, to play a sound on an event, attach an AudioSource component to a GameObject and call AudioSource.Play() inside the event handler method. This plays the assigned audio clip when the event occurs.
๐Ÿ“

Syntax

To play a sound on an event in Unity, you use the AudioSource component and call its Play() method inside the event function.

  • AudioSource: Component that holds and plays audio clips.
  • Play(): Method that starts playing the assigned audio clip.
  • Event function: Any method triggered by an event like a button click or collision.
csharp
AudioSource audioSource;

void Start() {
    audioSource = GetComponent<AudioSource>();
}

void OnEvent() {
    audioSource.Play();
}
๐Ÿ’ป

Example

This example shows how to play a sound when the player presses the space key. Attach an AudioSource with an audio clip to the same GameObject as this script.

csharp
using UnityEngine;

public class PlaySoundOnEvent : MonoBehaviour {
    private AudioSource audioSource;

    void Start() {
        audioSource = GetComponent<AudioSource>();
    }

    void Update() {
        if (Input.GetKeyDown(KeyCode.Space)) {
            audioSource.Play();
        }
    }
}
Output
When the space key is pressed, the assigned audio clip plays once.
โš ๏ธ

Common Pitfalls

Common mistakes when playing sound on events in Unity include:

  • Not attaching an AudioSource component to the GameObject.
  • Forgetting to assign an audio clip to the AudioSource.
  • Calling Play() on a null or uninitialized AudioSource.
  • Trying to play the sound before the component is ready (e.g., before Start()).

Always check your components and clip assignments in the Unity Editor.

csharp
/* Wrong way: No AudioSource component attached */
void OnEvent() {
    AudioSource audioSource = GetComponent<AudioSource>();
    audioSource.Play(); // This will cause a null reference error if AudioSource is missing
}

/* Right way: Ensure AudioSource exists and is assigned */
AudioSource audioSource;

void Start() {
    audioSource = GetComponent<AudioSource>();
    if (audioSource == null) {
        Debug.LogError("AudioSource component missing!");
    }
}

void OnEvent() {
    if (audioSource != null) {
        audioSource.Play();
    }
}
๐Ÿ“Š

Quick Reference

Tips for playing sound on events in Unity:

  • Always add an AudioSource component to your GameObject.
  • Assign an audio clip in the AudioSource inspector.
  • Use GetComponent<AudioSource>() to access the component in scripts.
  • Call Play() inside the event handler method.
  • Check for null references to avoid errors.
โœ…

Key Takeaways

Attach an AudioSource component with an audio clip to your GameObject.
Use GetComponent() to get the AudioSource in your script.
Call AudioSource.Play() inside the event method to play the sound.
Always check that the AudioSource is not null before calling Play().
Assign audio clips in the Unity Editor to avoid runtime errors.