- Home /
Is it possible to have mulitple soundtracks on a game?
Similar to when the voice in Black Ops 2 Zombies when the guy talks and then the music starts to play. Or when one soundtrack ends, another one starts and so on and so forth. No I do not mean to loop any track. Its more so like a radio type of effect that I want to have where songs just constantly keep playing and if possible for them to shuffle and repeat all. I'm not the best at coding so I could use a code for it. Thanks
Answer by maaz0011 · Jan 11, 2017 at 10:05 AM
yes it can, You have to use some UI buttons for it and create a script and add those component by making array its pretty much easy.
Answer by tanoshimi · Jan 11, 2017 at 10:33 AM
I think the term you need to search for is "jukebox", which would lead you to something like http://wiki.unity3d.com/index.php/JukeboxController
Sorry, I didn't mean like a jukebox. I meant when one track finishes, it moves onto another sorry. Similiar to a playlist of music
Answer by rabione · Jan 11, 2017 at 10:38 PM
(Answer by aldonaletto · Nov 04, 2013 at 10:52 PM)
First of all, add an AudioSource component to the object via menu Component/Audio/Audio Source, and delete the AudioSource declaration in your code (audio is a GameObject property). Change also the declaration of myMusic to Object[] (that's what LoadAll returns), and don't initialize it (there's no need). Finally, remove the type coercion from LoadAll (you can't coerce type of entire arrays this way). The code would become this:
using UnityEngine;
using System.Collections;
public class Music : MonoBehaviour {
Object[] myMusic; // declare this as Object array
void Awake () {
myMusic = Resources.LoadAll("Music",typeof(AudioClip));
audio.clip = myMusic[0] as AudioClip;
}
void Start (){
audio.Play();
}
// Update is called once per frame
void Update () {
if(!audio.isPlaying)
playRandomMusic();
}
void playRandomMusic() {
audio.clip = myMusic[Random.Range(0,myMusic.Length)] as AudioClip;
audio.Play();
}
}
NOTE: The audio clips must be inside a folder Resources/Music, or LoadAll won't find them.
Im a bit confused as to what you mean. Let's say that the name of one audioclip is "B7" and another is "PO9". Where would I put that in the code to make it work? Also that part about folders, would I make a folder, name it Resources, make another folder in it called $$anonymous$$usic?
I'm not the author of this code.
But what I guess, is that you can change your folder if you want, BUT change it whitin the code too.
This code plays all music clips on this folder. no matter their names.
So if i use this code it will play all of my audio clips in order or at random?
Your answer
Follow this Question
Related Questions
Sound gets loud in while playing game? 1 Answer
Trigger audio loop on beat with PlayScheduled 1 Answer
How To Turn Off Sound For Certain Objects? 1 Answer
Random Array of sounds for Random Game Object 1 Answer
Using stereo sound 1 Answer