• 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.

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

108 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

Related Questions

trying to add rarity to items, don't know whats wrong with this script 1 Answer

Random.Range is not changing? 1 Answer

How to put Random.range in float array? 3 Answers

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

Randomize values without exceeding a value? 3 Answers

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