- How would I go about making a list type variable (Or whatever its called)
- How would I pick a random sound effect to play from this list?
Trying to add some variety to my footsteps.
Trying to add some variety to my footsteps.
You would use an array probably:
http://docs.unity3d.com/Documentation/ScriptReference/Array.html
and the Random.Range(0,arrayName.length -1) function to access the sounds.
I found that, in javascript at least,i didn’t need the -1 in random.range. the max value is excluded, so 0 to array.length gives the right values straight out the box.
How do I access the sounds with the Random.Range if the Random.Range returns only a number?
Take one AudioClip Array and assign how many audio you want to play. Generate one random number with the use of generated number pick sound from Array and play it.
For Example:
public AudioClip[ ] myClip; // this is audioClip Array
Random.Range (1,5)
And play that audio.
The part you posted isn’t my problem.
My problem is playing the audio
var values : AudioClip[];
function Awake ()
{
audio.clip = Random.Range(0,values.length);
audio.Play();
}
Is what the current state is.
The problem is making it play the number in the array it picks.
var values : AudioClip[] = new AudioClip[5];
function Awake ()
{
audio.PlayOneShot(values[Random.Range(0,4)]);
}
Ofcourse calling the function in Awake will play the sound only once.
Here is the working example of playing random audio:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public AudioClip[] myClip;
// Use this for initialization
void Start () {
audio.clip = myClip[Random.Range(0,myClip.Length)];
audio.Play();
}
}
just add audiosource to that object.
Thank you everyone for your help!