• 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 AlejandroBoss · Jan 11, 2017 at 08:58 AM · randomsoundmusicshuffle

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

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

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
0

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

Comment
Add comment · Show 1 · 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 AlejandroBoss · Jan 11, 2017 at 09:28 PM 0
Share

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

avatar image
0

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.

Comment
Add comment · Show 6 · 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 AlejandroBoss · Jan 12, 2017 at 04:42 PM 0
Share

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 Music?

avatar image rabione · Jan 12, 2017 at 10:38 PM 0
Share

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.

avatar image AlejandroBoss rabione · Jan 12, 2017 at 11:06 PM 0
Share

So if i use this code it will play all of my audio clips in order or at random?

avatar image rabione AlejandroBoss · Jan 12, 2017 at 11:16 PM 0
Share

Random play.

Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Why can't I hear my audio? 5 Answers

Background music 0 Answers

Problems with array "shuffle bag" 1 Answer

Why does my background music get louder each time a scene is loaded 1 Answer

Cant import music and sound 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges