Dev Diary 005: Stumbling around Unity Scripting

  • warning: Creating default object from empty value in /home/buckman/public_html/neo/modules/advanced_forum/advanced_forum.module on line 492.
  • warning: Creating default object from empty value in /home/buckman/public_html/neo/modules/advanced_forum/advanced_forum.module on line 492.
  • warning: Creating default object from empty value in /home/buckman/public_html/neo/modules/advanced_forum/advanced_forum.module on line 492.
  • warning: Creating default object from empty value in /home/buckman/public_html/neo/modules/advanced_forum/advanced_forum.module on line 492.
  • warning: Creating default object from empty value in /home/buckman/public_html/neo/modules/advanced_forum/advanced_forum.module on line 492.
Matt Barton's picture

Well, I definitely think I've left all the easy stuff behind me...Way behind me. Coming to Unity Scripting from Gamemaker is like jumping from addition to differential calculus. I'm pretty much forced to copy and paste code from samples. What's frustrating is that I *feel* like I understand the concepts, but for some reason when I try to apply them I get lost in a sea of look-alike names and subtle but critical distinctions between objects, classes, components, etc.

To prepare myself for scripting, I spent some time reading the Unity Script Reference. A lot of this was familiar, and I get that each object can have its own scripts, variables, and so on. Without too much trouble, I was able to get objects to move and spin through scripts, but hit my first major roadblock when I started trying to integrate audio.

The project I set for myself was to have two objects on screen, a cube and a sphere, that would spin and loop a sound when you hovered over them with the mouse. Although you can do this work with rigidbodies, I thought it would be simpler just to use the transform tool to rotate them. This went off without a hitch.

Next step, adding the sound. Now I was quickly able to get a sound to play when you started the show, but getting them to play on command was tricky. I still don't get it, to be honest. What's the difference between an audio clip, an audio source, etc? I wasn't able to find a clear explanation of these concepts. From what I can see, there are lots of ways to play sounds, but don't get the rhyme or reason yet.

Here's the script I'm using to play the sounds here:
audio.PlayOneShot(beepsound,1);

First, though, I had to assign the variable beepsound:
var beepsound : AudioClip;

This only works if I manually go my sphere and cubes and select the right audio clip. I spent an hour or so trying to figure out how to do this from script, and am still completely befuddled. What if I had more sounds and wanted to choose a certain one to play at certain moments? It seems like changing beepsound to the right audioclip is the key, but I haven't figured out how to do that yet.

I also had no luck making the sound loop while you held the mouse over the object. I could get it to rapid-fire play, but that's not what I was going for. I tried working out a way to make the player delay a bit between plays, but couldn't figure that out either. This situation has me really upset, because if I can't even figure something simple like this out...Who am I kidding trying to make an RPG?

Just for kicks, I thought I'd see if I could publish my "work" using the Unity web player. I'm not sure how well it will work once things get really involved, but it seems to be working great for this. The potential is certainly exciting, though I'm wondering how big of a game you can get before the web player is not really a feasible option.

For my next project, I'm going to try to make a couple of objects using Blender and some quick and dirty animations for them. Then I'll try to bring these into Unity and find a way to trigger the different animations.

Unity Web Player | WebPlayer

Comments

Stardog (not verified)
To load something like an

To load something like an audio file you need a Resources folder. So, "beepsound = Resources.Load("Audio/audioname");" Stick that in the Awake/Start function.

http://unity3d.com/support/documentation/ScriptReference/Resources.Load....

Working with audio is a pain at first. AudioSource is what plays the sound, and an AudioClip is the wav/ogg file itself. Generally, you should only have one AudioSource on each object.

When saying audio.Play, you're accessing the AudioSource component of the object the script is attached to, which contains all the Play/Stop/etc functions. It's a shortcut word like "gameObject" or "transform".

You can do something like "audio.clip = beepsound" to change the clip of the AudioSource.

This is the same as saying "GameObject.Find("ObjectName").GetComponent(AudioSource).clip = beepsound".

Stardog (not verified)
That javascript at the bottom

That javascript at the bottom might be a bit wrong :)

Matt Barton
Matt Barton's picture
Offline
Joined: 01/16/2006
Got it! That works, thanks,

Got it! That works, thanks, Stardog. I ended up going with this code:
beepsound = Resources.Load("censor-beep-1");

I didn't realize for awhile that the folder name literally had to be "Resources." I had made one called "Sounds," and that didn't work at all.

Now if I could just figure out how to make it loop the sound right.

n/a
Matt Barton
Matt Barton's picture
Offline
Joined: 01/16/2006
One thing I was able to

One thing I was able to figure out with Gamemaker was "alarms" for timing things. So if, for instance, I wanted to make a beacon play a sound effect every 30 seconds, I'd start my alarm going in the creation event. The alarm event would play the sound and then call itself in 30 steps (or whatever). What's the right place to start looking to implement something like this in Unity?

n/a
Stardog (not verified)
Check out this:

Check out this: http://unity3d.com/support/documentation/ScriptReference/index.Coroutine...

I think the AudioSource component has a Loop checkbox though? Maybe look into that.

Otherwise you might have something like this:

function Awake()
{
MyPlayAudioFunction();
}

function MyPlayAudioFunction()
{
audio.Play();
yield WaitForSeconds(30);
MyPlayAudioFunction();
}

Daniel Biehl (not verified)
Some Unity Tutorial links that might interest you.

Hey Matt,

Here are some links you might find helpful in your quest to learn Unity. The first is a basic "getting to know" Unity and the second is a guy who starts from scratch and builds a hack-n-slash RPG style game. Maybe they'll be useful for you.

Link 1: Basic Unity Tutorials
Link 2: Hack-n-Slash RPG Tutorials

Hope this helps and thanks for the awesome blog posts and video interviews.

Dan

Alan (not verified)
These dev diary posts are

These dev diary posts are interesting. Keep up the good work.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.