Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 SergioSandiaz · Dec 20, 2016 at 05:48 AM · arrayprogrammingaudiosourceaudioclip

Issues with random music player

I was working on a "radio station", so I made my script, my GUI, everything fine, until I tested the script, I´m not sure what went wrong but you can watch the problem on the video:

Video

I´ve noticed about the IndexOutOfRangeException so the real problem here is why does the song change that way, of course here´s the script:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class DSN : MonoBehaviour {
     public AudioSource Audio;
     public AudioClip[] Songs;
     public AudioClip[] News;
     public AudioClip Estatica;
     public Slider Volume;
 
     public Slider Sintonizador;
     public Text Station;
 
     public Image VolumeImage;
     public Sprite[] VolumeSprites;
 
 
     void Start () {
     
     }
     
 
     void Update () {
 
 
         if(Audio.isPlaying == false && Sintonizador.value > 70f){
             int s = Random.Range(0, Songs.Length + 1);
             Audio.clip = Songs[s];
             Audio.Play();
         }
         if(Audio.isPlaying == false && Sintonizador.value < 30f){
             int r = Random.Range(0, News.Length + 1);
             Audio.clip = News[r];
             Audio.Play();
         }
 
         if(Sintonizador.value < 70f && Sintonizador.value > 30f){
             Audio.clip = Estatica;
             Audio.loop = true;
             Audio.Play();
         }else{
             Audio.Stop();
             Audio.loop = false;
         }
 
         Audio.volume = Volume.value;
         Station.text = Sintonizador.value.ToString("F1");
 
         if(Volume.value == 0f){VolumeImage.sprite = VolumeSprites[0];}
         if(Volume.value > 0f && Volume.value < 0.33f){VolumeImage.sprite = VolumeSprites[1];}
         if(Volume.value > 0.33f && Volume.value < 0.66f){VolumeImage.sprite = VolumeSprites[2];}
         if(Volume.value > 0.66f){VolumeImage.sprite = VolumeSprites[3];}
     }
 }

Just to clarify, the array "News" isn´t setted up, so, just focus on the "Song" array

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 Wolfrik_Creations · Dec 20, 2016 at 06:29 AM

You're doing:

int s = Random.Range(0, Songs.Length + 1);

That's why the index is out of range, try:

int s = Random.Range(0, Songs.Length);

Comment
Add comment · Show 3 · 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 SergioSandiaz · Dec 21, 2016 at 12:00 AM 0
Share

Yeah, it´s because I read somewhere that if you use Random.Range with an int it will never return the last item in the array, that´s why I put + 1, but if it can work without it, I´ll delete it :)

avatar image Barruchi SergioSandiaz · Dec 21, 2016 at 04:29 AM 0
Share

Yeah... I read about it too... But seems this doesn't apply to var.Lenght (array) var.Count (List).

avatar image SergioSandiaz Barruchi · Dec 21, 2016 at 07:04 AM 0
Share

Thanks for your help, I´ve already fixed it :)

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

73 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

Related Questions

Multiple Cars not working 1 Answer

Play sound from an array, not random, and only once 3 Answers

Instanced Prefabs: Audio clip array out of range 1 Answer

How to play "audiosource" component in a prefab from script? 0 Answers

AudioClip dont listened in script component 1 Answer


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