Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 $$anonymous$$ · Jun 23, 2019 at 11:49 AM · random.rangemusicrandomizationarray list

How do I randomize my Music played? The opening song is always the same song and I don't know why.

I made a Script which plays music every 2 to 5 minutes. Each time, the music gets randomly picked from the public AudioClip[] MusicClips array. The Line MusicSource.clip = MusicClips[Random.Range(0, MusicClips.Length)]; in the method PlayMusic() always picks a random Song from the Array before it gets played.

My only Problem: Despite randomness, the first song that plays when the game starts, is always the 1st Song from my array. Never does the game start with the 2nd, 3rd, or any further Array element. It's always the 1st, but it's not supposed to be only the 1st.

How do I also randomise the opening song and not just any song after?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MusicSystem : MonoBehaviour
 {
     public AudioClip[] MusicClips;
     public AudioSource MusicSource;
     
     // Start is called before the first frame update
     void Start()
     {    
         StartCoroutine (MusicTimer()); //Start Music
     }
     
     // void Update() { if (Input.GetKeyDown(KeyCode.F5)) { PlayMusic(); }  } //This Line of Code is for Debug Purposes
     
     IEnumerator MusicTimer()
     {
         yield return new WaitForSeconds(1); //Wait 1 Second
         
         PlayMusic();
         
         yield return new WaitForSeconds(Random.Range(120, 300)/*2 to 5 Minutes*/); //Wait Until Next Song
         
         StartCoroutine (MusicTimer()); //Restart this Coroutine so you can hear the next Song after some more time
     }
     
     void PlayMusic()
     {
         MusicSource.clip = MusicClips[Random.Range(0, MusicClips.Length)];
         
         MusicSource.Play(); //Play Music
     }
 }

The Script is on this Game Object: alt text

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by $$anonymous$$ · Jun 23, 2019 at 02:22 PM

Oops! Even the Opening Song IS randomized! For me, it just did not happen to choose the 2nd or 3rd Song until now!

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

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

109 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 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 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 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 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

Is there a better way for randomization for triggering a percentage of the time 1 Answer

Randomize values without exceeding a value? 3 Answers

How to make certain elements in an array rarer when using random selection? 4 Answers

Is it possible to always make animations play differently? 0 Answers

How to make random number generation more random? 2 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