Blog: Game Audio Scripting With Unity - Part 1

US Game audio development platform revealed      03/03/14

Basic Scripting

Let's add some really basic scripting to our Scene. Normally you would be integrating audio into a scene that was assembled by designers or developers, so it would have GameObjects already placed, but for testing purposes let's add a sphere to our scene.

From the Game Object menu: Create Other -> Sphere

Next, drag sound effect GameObject into the Sphere GameObject:

Finally, add a new C# script component to the Sphere:

  • Select the Sphere in the Hierarchy
  • From within the Inspector pane click "Add Component"
  • Select "New Script"
  • Name the script "Sphere" and select "CSharp" as the language

When you're done it should look like this:

Double-click the script name (Sphere) and Unity will open its integrated code editor, MonoDevelop. If it looks daunting, don't worry: we'll only be adding a few lines of code.

We're going to tell Unity to play play the sound effect when the player clicks the sphere. We'll do this by adding an "OnMouseDown" event. Add this to the bottom of the script, but above the last closing brace.

     void OnMouseDown() {
          transform.Find ("sfx").audio.Play ();
     }

When you're done it should look like this:

That's it! You've just written your first C# function. Note that OnMouseDown has a special meaning to Unity: it's called when the left mouse button is pressed on the sphere (more specifically, on the SphereCollider.)

The code within the function tells unity to "Find the GameObject called 'sfx' and Play its Audio Source."


Unity has all sorts of special functions like this which can be found in the comprehensive script reference.

These special functions are called "Messages", and here are the possible messages for SphereCollider component:

So for example, you could use OnCollisionEnter to play a sound when the GameObject is hit by another GameObject, and we'll be looking at that in the next post.

But where's "OnMouseDown"? Well, there are a whole set of messages that apply to all GameObjects.

Finally, let's play our scene again. Now when you click the sphere in the middle the sound effect audio clip will retrigger:

It's not much to look at, but it's a good start!
That's it for this post, but next month I'll look at some more complex scenarios, like adding reverb to your scenes and scripting dynamic and conditional audio events.

Thanks for reading!

Find out more about Unity

Adam McLellan, AKA Snug, is a DJ and producer based in Halifax, Nova Scotia, Canada. Since a young age he's been fascinated by the intersection of art and technology. When he's not producing or performing he's sharing his knowledge and ideas through teaching, writing for his personal blog (snugsound.com)

Send us your News: If you have a story or product you'd like us to cover - email news@sonicstate.com or use our contact form or join our Discord and post in #cool-finds


Get the latest headlines, offers and more in our fly sonicstate newsletter - delivered to your in box every week
More From: UNITY
Even more news...


 

Get Our Newsletter


More...

Eventide H910 and more


5 MINUTES WITH ODD-1 Granular Instrument 

Multiple engines of granular joy


Broke Beats pt2 - A £30 DAW & Microcomputer 

M5Stack Cardputer ADV is an ESP32 S3-Powered Wonder


Sega Genesis/Mega Drive Sounds on the Carbon 8 

Creating Twangy Sega Tones on Modal's Synth


Presentation: Sonible Smart Chain Mix Plug-in 

Balance and process multi-track sessions


UDO DMNO ON - Desktop DMNO System 

Desktop DMNO and expanders...