I’m new here, so please be nice. 
I am having an awfully hard time making a particular sound source play on a click event. I’ve searched high and low for what I’m looking for, but every example and question seems to be related to a soundsource that is attached to an object.
What I want to do is actually play a sound bite depending on certain conditions within the game. So, I understand how to check the conditions, but damned if I know how to create a new instance of the sound object then play it.
I’ve tried :
var mysource : AudioSource;
var myclip : AudioClip;
mysource.clip = myclip;
mysource.Play();
But that just throws an error, “Object reference not set to an instance of an object”. I guess the problem is that “myclip” actually isn’t associated with anything. So, in my Project view, I have a audio clip called “milkshake” - how do I make “myclip” refer to “milkshake.wav”?
I’ve tried all sorts of other crazy things like trying to .Play() the actual object itself (I didn’t expect that to work, and it didn’t).
I know the more seasoned of you are probably laughing at the simplicity (or stupidity?) of this question, but I’m just not getting how this audio scripting works - I can’t see any examples that tell you how to create this variable properly either through variables or functions.
Regarding this particular question:
You can drag the audio clip from the project pane onto the ‘myclip’ field in the inspector, of you can select the clip from the pop-up menu associated with the ‘myclip’ field.
Generally speaking, there are several different ways to achieve what you’re wanting. Without knowing more about the problem I can’t really recommend a specific approach, but you might want to look into the functions AudioSource.PlayOneShot() and AudioSource.PlayClipAtPoint().
PlayClipAtPoint() will create a new game object that will play the specified sound and then remove itself from the scene once the sound has finished playing. PlayOneShot() is a non-static member function that you would call on an existing audio source component.
If you attach an audio source component to your game object, it should be available from scripting via the inherited member variable ‘audio’. If associating an audio source component with the game object doesn’t work for you for some reason though, there are other approaches you can take (again though, without understanding the problem better I can’t really make any specific suggestions in that area).
I’ll explain in basic terms what I’m doing :
If the user clicks and object, it runs the OnMouseDown event. The event needs to then play a particular audio file, depending on certain conditions within the game. (Say, it plays file1 if the character is within a certain area, or it plays file2 if the character is in another area).
So… Just by using the code:
mysource.Play();
…works just fine, if I never want to change which sound is being played.
So my question is - how do I change the audio clip at runtime. (I don’t mean how do I write an IF statement, I get that bit) The script has two variables, mysource and myclip. I’m sure I could just create a whole bunch of variables and change the mysource.clip value, but that just seems incredibly stupid.
I could create an array, but again how am I meant to insert clips into that array without creating a whole bunch of hokey variables first? In my experience with programming languages, you can insert instances of an object into an array by assigning it like:
array[index] = new ClassName(createParams);
Surely Unity can do that without having to preset everything in the inspector.
I’m not sure I understand the problem fully. Is there any particular reason you’re not just using the ‘audio’ member field? What’s the purpose of the separate ‘mysource’ variable? Where does the audio source that’s assigned to it live?
If you create public arrays in your class or script, as long as the element type is something Unity can serialize, the array will show up in the inspector. (You can use the generic List class as well.) Creating an array of sound clips and playing them back through a single audio source seems reasonable to me (as long as you don’t need the sounds to be able to overlap - in that case you’ll have to use a different method).
yeah, you need to make an array, and then use the ID of the array to switch the clip, i.e.
then call that function with the appropriate ID, i.e. PlayClip(1);
that little bracket in the variable declaration makes it an array, and you’ll see a size value in the inspector to determine it’s length. Hint: zero is always the first value.
oh, and welcome to the forum! hth
@Jesse
I’m not sure how else to explain the problem. I read my last post and it makes perfect sense to me. I want to play an audio clip which is determined at runtime (and not set in the inspector) when someone clicks on an object (like a cube)
I’ll keep bashing my head against the wall 'til I figure it out. Thanks anyway. 
@polytropoi
Yep, I get how arrays work, The problem is I dont know how to set sounds INTO the array.
So, in your example, I figure inserting a sound into the array would be something like:
theClips[0] = <something?>
I really don’t want to have to set this in the inspector. (And I don’t see how you insert objects into an array in the inspector anyway, I only see the maxvalue, which makes sense - I understand that).
You just drag and drop the AudioClip into the array (which should be an AudioClip [ ] ).
I don’t understand the problem. Why don’t you simply make it simple ? An AudioClip variable for each sound, and/or an AudioClip [ ] array ?
<something?> must be an object of type AudioClip. You get, retrieve it by any way you want, but it must be an AudioClip.
Hehe, you wrote it…of course it makes perfect sense to you 
Anyway, it’s not that I don’t get that you want to play one of several sounds in response to an in-game event; I just wasn’t sure as to what part of the process was causing you problems.
To populate the array in the inspector, you first set the ‘size’ field to whatever number of audio clips you need. The array will then expand to show the specified number of elements, at which point you can drag audio clips onto the fields (or use the pop-up menus).
As for assigning audio clips programmatically, you might be able to use Resources.Load() for that (although I haven’t tried it myself). There might be other methods that I’m not thinking of at the moment though.
2 kinds of arrays in Unity: http://unity3d.com/support/documentation/ScriptReference/Array.html
Unless you need to resize the array at runtime, you should use a built-in array in the way described above. Use the syntax I quoted, and as others said, set the size field for the array in the inspector, then drag your clips into the slots created in the inspector. I’d attach this script to an empty named something like SoundManager, then on my active objects like the cube you mentioned, have a script that messages the SoundManager object, calling the play function with the desired array index when the object is clicked.
Ahhh okay, I get it - I feel like an idiot!! Although I still haven’t quite figured out how to populate the array at runtime, using the Inspector is good enough for now and is giving me the results I’m after.
I’ll go back and hide in my newb cave now. Thanks for everyone’s help! 