Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kaloss · Aug 08, 2011 at 08:23 AM · audioclip

Play a list of tracks in random order

Hi

I want to play a list of music tracks in random order right from the start of the game. Here is what I get so far....

 var music : AudioClip [];
 function Start () 
 {
 if(!audio.playOnAwake) 
 {
 audio.clip = music[Random.Range(0,music.length)];
 audio.Play();
 audio.loop = true;
 }
 }

So it does pick a random track to play, but when that track is finished, it doesn't play the next random one, it just repeat that track.

Help me please :)

Thanks!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MaxLohMusic · Feb 05 at 12:33 AM 0
Share

How/when does "music" array get populated? (Edit: Just drag/drop tracks into the exposed array via Unity editor)

8 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by vividhelix · Oct 31, 2013 at 05:46 AM

You can do something like this, once the song finishes it will play another one at random:

 void PlayNextSong(){
     audio.clip = music[Random.Range(0,music.length)];
     audio.Play();
     Invoke("PlayNextSong", audio.clip.length);
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
3

Answer by davem250 · Mar 23, 2014 at 11:09 PM

This is what i've learned from this page, it is in c# btw :

 using UnityEngine;
 using System.Collections;
 
 public class RandomAudio : MonoBehaviour
 {
     public AudioClip[] soundtrack;
 
     // Use this for initialization
     void Start ()
     {
         if (!audio.playOnAwake)
         {
             audio.clip = soundtrack[Random.Range(0, soundtrack.Length)];
             audio.Play();
         }
     }
     
     // Update is called once per frame
     void Update ()
     {
         if (!audio.isPlaying)
         {
             audio.clip = soundtrack[Random.Range(0, soundtrack.Length)];
             audio.Play();
         }
     }
 }

it is very simple yet it works it loops between the different numbers i have just tried it out and it works fine! use if you like :D

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cossSAH · Dec 21, 2015 at 07:56 PM 0
Share

dude thx.. ppl just be careful about your script name and class name must match EXACTLY, including capital letters, spaces, etc.

avatar image gamemakerdude15145 · Feb 20, 2021 at 01:01 PM 0
Share

They also can't have spaces.

avatar image
2

Answer by renanss · Oct 31, 2013 at 05:09 AM

//This is my version of Audio Playlist.

 using UnityEngine;
 using System.Collections;
 
 public class MusicBg : MonoBehaviour {
 
 private static MusicBg instance = null;
 public AudioClip[] musicbg;
 private int i;
     
     
     public static MusicBg Instance
     {
         get { return instance; }
     }
     
     
     void Awake() {
         if (instance != null && instance != this) {
             Destroy(this.gameObject);
             return;
         } else {
             instance = this;
         }
         DontDestroyOnLoad(this.gameObject);
     }
  
     void Start()
     {
     i= Random.Range(0,musicbg.Length);
     StartCoroutine("Playlist");
     }
     
 IEnumerator Playlist()
     {
         while(true)
         {
             yield return new WaitForSeconds(1.0f);
             if(!audio.isPlaying)
             {
                 if(i != (musicbg.Length -1))
                 {
                     i++;
                     audio.clip = musicbg[i];
                     audio.Play();
                 }
                 else
                 {
                     i=0;
                     audio.clip= musicbg[i];
                     audio.Play();
                 }
             }
         }
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image crutchc · Mar 31, 2018 at 02:01 AM 0
Share

DId you apply the script to the main camera? or was it to just an audio source in the map itself?

avatar image Reefer · Dec 10, 2020 at 08:23 PM 0
Share

If I walk it stops playing of that song, any ideas how to fix?

avatar image Reefer · Dec 10, 2020 at 08:55 PM 0
Share

Ahh fixed it.. Np anymore

avatar image
1

Answer by unluckyBastard · Aug 08, 2011 at 08:50 AM

it's because it's never picking a new value for random range, it's only called once in start

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kaloss · Aug 08, 2011 at 10:02 AM 0
Share

so what should I do now?

avatar image Waz · Aug 08, 2011 at 10:06 AM 0
Share

Try writing something in Update(). Clue: don't loop, check if still playing before playing a new one.

avatar image unluckyBastard · Aug 08, 2011 at 10:35 AM 0
Share

var music : AudioClip [];

function Update(){ if(!audio.playOnAwake){ audio.clip = music[Random.Range(0,music.length)]; audio.loop = true;

if(!audio.isPlaying){ audio.Play(); } } }

try that ^ didn't test it but I'm pretty sure it should work

avatar image kaloss · Aug 08, 2011 at 11:25 AM 0
Share

I don't think that the Update will work. Just tried it and the sound can not be played, just some weird sound. I think this is because Update is called every second?

avatar image unluckyBastard · Aug 08, 2011 at 10:48 PM 0
Share

but the script checks if audio is playing before it plays a new one...I guess you can put a yield statement using the clips length

avatar image
0

Answer by ArinFaraj · Mar 30, 2014 at 08:35 PM

 var Musics : AudioClip[];
 var currentM = 0;
 var mMusic : AudioClip;
 var mNextMusic : AudioClip;
 var MusicP : AudioSource;
 //var MusicTime = 100;
 
 function Start () {
     
     if (currentM >= Musics.length) currentM = Musics.length-1;
     /*for (var i=0; i<Musics.length; i++)
     DisableMusic(Musics[i]);
     */
     SelectMusic(Musics[currentM]);
 }
 function Update () {
     var IN = Input.GetButtonDown("Music");
     if(IN )//||MusicP.time>MusicTime)
     {
         SwitchMusic(1);
     }
     MusicP.clip = mMusic;
     if(this.ignoreListenerVolume){}
     
     }
     
     function SwitchMusic (iDir : int)
     {
         if (Musics.length < 2) return;
         
         
         currentM += Mathf.Sign(iDir);
         if (currentM < 0) currentM = Musics.length-1;
         else if (currentM >= Musics.length) currentM = 0;
         
         SelectMusic(Musics[currentM]);
         MusicP.Play();
     }
     
     function SelectMusic(Music : AudioClip)
     
     {
         if (mMusic)
         {
             mNextMusic = mMusic;
         }
         
         mMusic = Music;
         //MusicP.Play();
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

play audio from frequency and amplitude file 0 Answers

Audio clip.GetData not working 0 Answers

Make the audio manager select from an array of sounds 1 Answer

How to change a File Resource .ogg using IO.Stream into an AudioClip with a C# Script?,Anyone know how to turn an IO.Stream into an AudioClip through a C# script? 0 Answers

Getting audio to play on collision 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges