I’m new to Unity and all this stuff and what I’m looking for is a concise tutorial (or script) for triggering random sound arrays for multiple objects. What I have is basically a Pong Style game with 3 balls and I want each ball to have a different array of sounds that it randomly chooses from when it collides with something. I have tried several methods and none of them seem to work. I have all the audio files stored in their own folders inside an an ‘Audio’ folder in my assets but nothing seems to work for me. I’m completely confused about the way arrays are supposed to work in Unity because everybody has a different method. Which one is simplest and works best? Thanks for your help
Hello there and welcome to the Unity community!
What you can try is to create an array in your script that is playing the sounds and store your audio clips by dragging and dropping it into the array in the inspector (make sure you set the array to public!)
public AudioClip[] myAudioClips;
Then you can roll a random number using Random.Range
int rand = Random.Range(0, myAudioClips.Length -1); // -1 from length because max is inclusive
Now that you’ve got your random number, you just need to assign the clip into your audio source and play it.
myAudioSource.audio.clip = myAudioClips[rand];
myAudioSource.audio.PlayOneShot();
All the codes are in C#. I’m sorry but I can’t convert to JS that well. Maybe a JS programmer can come along and convert and explain further. Good luck and happy coding!
Yeah, my project is in JS. This is my first game using Unity and I’m totally confused. This type of thing is easy as pie in UDK. I don’t get where the script is referencing my folders or where the script references the actual sound files.
Just wanted to say that actually you should use myAudioClips.Length. Not minus 1. When you’re using int’s, Random.Range() is actually exclusive not inclusive. It’s only inclusive when you use floats. I think Unity just likes to be confusing that way. ![]()
http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html
Oh yeah you’re right. I blame my lack of sleep =P
@KennyRose: The script does not reference anywhere in the folder unless you drag and drop the object from your folder and into the object via the inspector to assign the variable. If you want to load objects from a folder, you can take a look at Resources.Load.
Yeah, I got it working last night. Unfortunately that was AFTER i posted some really crappy code to another forum. LOL I figured out that I had put lines of code in the wrong places and not enough brackets (hate those brackets!).
There’s an advantage in using Unity over UDK*, I think. If you try to sell a product that you made using UDK, don’t the developers of UDK get about 25% of the revenue for the product that you created, or something crazy like that?
Anyways, I think this was the code you were after:
var myAudioClips[] : AudioClip;
var rand : int = Random.Range(0, myAudioClips.Length);
myAudioSource.audio.clip = myAudioClips[rand];
myAudioSource.audio.PlayOneShot();
The differences between Javascript and C-sharp seem to be structural.
int i = 7; // C-Sharp... the estranged child of C++.
var i : int = 7 // Java-Script.
- Don’t misunderstand… I would love to work with an interface like UDK (alas, in mac-world, it doesn’t work) just see how it works. But I would sooner write my own engine than sell a product made with one that required me to fork over a significant percentage of the revenue.