- Home /
Looking for a method for implementing multiple random sound arrays.
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.
Answer by Matthew Scott · Apr 29, 2013 at 05:42 AM
var audioClips : AudioClip[];
function Start()
{
audio.PlayOneShot(audioClips[Random.Range(0, audioClips.Length)]);
}
This is a javascript version of what I would do.
To populate your array with your audio clips, attach this to a gameobject, and then in the editor, look at the inspector panel and you will see the array appear there. Adjust the size value to open new slots in the array as needed and drag your audio clips in them.
Check these links in the docs;
http://docs.unity3d.com/Documentation/ScriptReference/Array.html http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html
Read up about Built-in ararys. (What I used the above example)
There fast and efficient and not hard to use once to get used to them.
I'll try this when I get home. I think my problem had something to do with that first line of code. I wasn't seeing the array in the inspector. $$anonymous$$aybe I missed it, but thanks for the advice. This looks like what I needed.
Answer by KennyRose · Apr 29, 2013 at 11:00 PM
Yeah, I'm not getting access to the array in the inspector for some reason. Here is what I have so far.
var mainGameScript : MainGame;
var BallSparks : GameObject;
var audioClips : AudioClip[];
function Start ()
{
audio.PlayOneShot (audioClips[Random.Range(0, audioClips.Length)]);
}
function Awake ()
{
rigidbody.AddForce(6,0,6, ForceMode.Impulse);
InvokeRepeating ("IncreaseBallVelocity",2,2);
}
function Update ()
{
if (transform.position.z < -9)
{
mainGameScript.GameOver();
//Application.LoadLevel ("ComputerBluesMenu");
}
}
function IncreaseBallVelocity()
{
rigidbody.velocity *= 1.05;
}
function OnCollisionEnter (collision : Collision)
{
Instantiate (BallSparks,transform.position, transform.rotation);
audio.Play();
}
}
Hmm, you should do, are you absolutely sure your script is attached to a gameobject in your scene? Even an empty game object would do. When you click on the object in the hierarchy it's properties will show in the inspector panel, under where the script is it should absolutely be there.
Should look like this...
Here i entered 3 as the size to open up 3 slots.
Double check this, if it's still not working you can assign them individually through code but it seems a little overkill because you can do it through the inspector.
Your answer
Follow this Question
Related Questions
Function Update play sound once 1 Answer
Audio Distortion effects? 0 Answers
Add Audio Source to a Prefab 2 Answers
2nd Audio Clip Not Playing 0 Answers
Need help with audio files causing unity to freeze 0 Answers