• 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 polda602 · Jul 14, 2015 at 12:30 AM · audiomusicmp3

Randomly mp3 sript, playing just first song

Hi, please help me with this i don't know what i'm doing bad. I'm trying to make music, that play randomly, but i don't know how to do that. Thanks.

 using UnityEngine;
 using System.Collections;
 
 public class Music : MonoBehaviour {
 
     public Object[] myMusic; // declare this as Object array
     void Awake () {
         myMusic = Resources.LoadAll("Music",typeof(AudioClip));
         GetComponent<AudioSource>().clip = myMusic[0] as AudioClip;
     }
     
     void Start (){
         GetComponent<AudioSource>().Play ();
     }
     
     // Update is called once per frame
     void Update () {
         if(!GetComponent<AudioSource>().isPlaying)
             playRandomMusic();
     }
     
     void playRandomMusic() {
         GetComponent<AudioSource>().clip = myMusic[Random.Range(0,myMusic.Length)] as AudioClip;
         GetComponent<AudioSource>().Play();
     }
 }

   
Comment
Add comment · Show 3
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 Dibbie · Jul 14, 2015 at 12:59 AM 0
Share

To me, that seems over complicated, I wouldnt do the whole resources stuff. Id keep it simple:

public AudioClip[] playList;

  void Start (){
      playRandom$$anonymous$$usic();
  }

  void playRandom$$anonymous$$usic() {
      GetComponent<AudioSource>().clip = playList[Random.Range(0,playList.Length - 1)];
      GetComponent<AudioSource>().Play();
  }


Then every time you run your application, it will randomly play a track, but its possible you could get the same "random" track 2 or 3 times in a row, because of how the Random class works.

avatar image polda602 · Jul 15, 2015 at 07:34 PM 0
Share

it's not working :/

avatar image Dibbie · Jul 15, 2015 at 10:50 PM 0
Share

Well, make sure that the object you have this script on, 1. Has an audio source, and 2. the script has audio clips/mp3 files in its array, then it should work fine with no problems.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by InfernoZYB · Jul 14, 2015 at 01:16 AM

Hmmm Try using an audioclip insted of an object. Try this. Never tried this but should work.

 public AudioClip[] Sounds;
     
     void Update(){
         if(!GetComponent<AudioSource>().isPlaying){ PlayRandomMusic(); }
     }
     
     void PlayRandomMusic(){
         int i = (int)Random.Range (0, Sounds.Length);
         GetComponent<AudioSource>().PlayOneShot(Sounds[i], 1F);
     }
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 polda602 · Jul 15, 2015 at 07:35 PM 0
Share

this script makes that, all song are playing together... but thanks

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Large music files make asset importing slow / game large. Compression ideas? 0 Answers

How can I do music selection for Windows and Mac? 0 Answers

What are the parameters to the AudioClip.create() function 0 Answers

Audiosurf Visualization 2 Answers

prevent unity from cutting phone music off? 0 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