- Home /
What is the most efficient way to play random audio tracks?
Hello, everyone. I am asking for the most efficient way (as in the least amount of code) to pick between 5 different audio tracks and play the selected one. Because the only ways I can think of doing so are way too long. Thanks for your help!
Answer by SharkoFR · Apr 29, 2018 at 07:56 PM
Hi! this is the best way:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class tuto : MonoBehaviour { public AudioSource AS; public AudioClip[] tracks; public void yourVoid() { AS.clip = tracks[Random.Range(0, tracks.Length)]; AS.Play(); } }
Tell me if you have question.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class tuto : $$anonymous$$onoBehaviour {
public AudioSource AS;
public AudioClip[] tracks;
public void yourVoid()
{
AS.clip = tracks[Random.Range(0, tracks.Length)];
AS.Play();
}
}
this is the code
Your answer
Follow this Question
Related Questions
Random, 3d sound. 1 Answer
Poor performance when using AudioSource.Play 0 Answers
audio from mic, db value keeps getting stuck at - infinity ? 0 Answers
Find all Objects/Scripts that access a given audio!! 1 Answer
OnTriggerExit stop audio 1 Answer