How to play sounds in Unity?

  • Copy and paste your audio files into your Unity Project. These are now Audio Clips;
  • Add an Audio Source component to an empty gameObject or any other gameObject in your scene;
  • In the Inspector, find the Audio Clip property on the Audio Source component and assign a clip, either by dragging one from the Project Window or by clicking the small circle icon to the right of the Inspector property, then selecting a clip from the list.
Creating a new audio source
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlaySound : MonoBehaviour
{
AudioSource MyAudio;
void Start()
{
//Get a reference to the Audio Source Component
MyAudio = GetComponent<AudioSource>();
//Play the Audio Clip at start
MyAudio.Play();
}
// This function is called for rendering and handling GUI (Graphic User Interface) events.
void OnGUI()
{
//Create a pause button
if (GUI.Button(new Rect(10, 70, 150, 30), "Pause"))
{
//Pause the Audio Clip
MyAudio.Pause();
}
//Create an unpause button
if (GUI.Button(new Rect(10, 170, 150, 30), "Continue"))
{
//Unpausing the Audio Clip
MyAudio.UnPause();
}
}
}
Project global Audio settings
Project global Audio settings

--

--

--

Computer Science Engineer and Cisco Instructor

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Using OpenEBS to build a true on-premise container service

Why you need a proper QA process

Axure Plugin for Jira, Confluence, Microsoft Teams and Slack

Creating a FARM Stack Dev Environment with Docker Compose — Part 3 of 3: React

The logos for the FastAPI, React, and MongoDB companies of the FARM stack.

Variable Scope and Access in Ruby: The Important Parts

5 tech trends that foretell the future of Java in 2022

How I Got An Appreciation Letter From Harvard University

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ouzani Abd Raouf

Ouzani Abd Raouf

Computer Science Engineer and Cisco Instructor

More from Medium

3D Survival FPS game

City Planner- In Virtual Reality

The 5 best free Unity SDK for Mobile Unity 3D Developers

Enemy Lasers!