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)




< More From: UNITY
Even more news...


 

Want Our Newsletter?



More...

Featured Video

Brand new super-synth from UDO


Featured Video

Virtual knobs and jacks controlling over 160 virtual modules


Featured Video

Now ready for preorder


Featured Video

The sampler pedal that listens and moves with you


Featured Video

The Mantis has arrived


Featured Video

Bi-timbral noise-based soft synth with a wide range of possibilities


New developments for Waldorf's M 

Waldorf's hybrid synth has quite the development story


Pittsburgh Modular's latest release


5 Firmware Updates that Totally Changed the Game 

Revisions that turned synths into brand new machines


Moog At The Super Bowl 

The Avila Brothers talk about their journey to the recent Super Bowl Halftime Show


5 MORE Firmware Updates that Totally Changed the Game 

Part 2 on revisions that made synths into brand new machines


SUPERBOOTH 2024: UDO - Super 8 Polysynth 

Brand new super-synth from UDO


Hey there, we use Cookies to customize your experience on Sonicstate.com