Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Jesus_Freak · Feb 25, 2011 at 10:19 PM · arraymenuarray-out-of-range-except

Audio loop half working

hey, i've been working on my own music loop to play throughout the game. the control is on a "static" object, meaning it never gets destroyed on awake.

So what i want is a pause menu, and it has two buttons, a next and back button. which will change to the next, or last song. and i got that sorta working. but what's supposed to happen is that no matter how many times you press either button consecutively, it will loop through the array of songs. and it works for the back button. but the next button, after the last song in hte array, it ALWAYS says that "IndexOutOfRangeException: Array index is out of range."

caould you please look through my code and help me figure out what's wrong?

Thanks you!

static var Paused : boolean = false; var soundTrack : AudioClip[]; var myFont : Font; var currentTrack = 0; var e : Vector4[]; var TrackID : int; private var dir : int = 0; private var timer : float; private var timeStarted = false; private var notInput = true;

//awake fn function Awake () { dir = 0; // Make this game object and all its transform children // survive when loading a new scene. DontDestroyOnLoad (transform.gameObject); }

//GUI fn function OnGUI() { if(Paused) { GUI.skin.box.font = myFont; GUI.Box(Rect((Screen.width / 2) - (180), (Screen.height / 2) - (260),(Screen.width / 4)+(180), (Screen.height / 4)-(100)),"Your game is paused, press (r) to resume.");

if(GUI.Button(Rect((Screen.width / 2) + e[0].x, (Screen.height / 2) + e[0].y, (Screen.width / 4) + e[0].z, (Screen.height / 4) + e[0].w), "Next Song>>")) { dir = 1; notInput = false; CheckID(); } if(GUI.Button(Rect((Screen.width / 2) + e[1].x, (Screen.height / 2) + e[1].y, (Screen.width / 4) + e[1].z, (Screen.height / 4) + e[1].w), "<<Last Song")) { dir = -1; notInput = false; CheckID(); } } }

//my fn function CheckID() { switch(dir) { case -1: switch (TrackID) { case 0: notInput = true; dir = 0; break; case 1: currentTrack = soundTrack.length -1; TrackID = 4; break; case 2: currentTrack--; TrackID = 4; break; case 3: currentTrack--; TrackID = 4; break; case 4: TrackID = 0; break; } break; case 1: switch (TrackID) { case 0: notInput = true; dir = 0; break; case 1: currentTrack++; TrackID = 4; break; case 2: currentTrack++; TrackID = 4; break; case 3: if(currentTrack >= soundTrack.length -2) { currentTrack = 0; } currentTrack = 0; TrackID = 4; break; case 4: TrackID = 0; break; } break; } }

//update fn function Update () { if(audio.clip != soundTrack[currentTrack]) { audio.Stop(); audio.clip = soundTrack[currentTrack]; audio.Play(); } var minutes : float = Mathf.Floor(timer / 60); var seconds : float = Mathf.RoundToInt(timer%60);

if(Input.GetKeyDown("p")) { Paused = true; } if(Input.GetKeyDown("r")) { Paused = false; } if(currentTrack >= soundTrack.length -2) { TrackID = 3; } if(currentTrack <= 0) { TrackID = 1; } if(currentTrack > 0 && currentTrack < soundTrack.length) { TrackID = 2; }

if(!audio.isPlaying) { if(minutes > 1 || seconds > 120) { dir = 1; CheckID(); } audio.clip = soundTrack[currentTrack]; audio.Play(); } if(timeStarted && notInput) { timer += Time.deltaTime; } if(timeStarted && !notInput) { timer = 0; } }

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 Uriel_96 · Feb 25, 2011 at 10:31 PM 0
Share

does the variable currentTrack gets bigger than the size of the array of soundTrack?? maybe could be that

avatar image Jesus_Freak · Feb 25, 2011 at 10:32 PM 0
Share

yes, i tried logging what currentTrack was. and i started with only 5 items in the array (0 through 4) but currentTrack does register as 5, which would put it over the top, but how do i fix it? i tried adjusting what made trackId = 3 (to set it to zero ins$$anonymous$$d of 5), but that didnt do anything..... so im not sure how to fix it....

avatar image Jesus_Freak · Feb 25, 2011 at 10:44 PM 0
Share

never$$anonymous$$d... i fixed it, where i said to make it normally go to the next song, the factors for that to happen where to inclusive. so ins$$anonymous$$d of "currentTrack > 0 && currentTrack < soundTrack.length", i changed it to "currentTrack > 0 && currentTrack < soundTrack.length - 1" and that's all i needed. thanks for looking at it for me though!

0 Replies

· Add your reply
  • Sort: 

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

No one has followed this question yet.

Related Questions

IndexOutOfRangeException: Array index is out of range. 2 Answers

Array index is out of range 1 Answer

IndexOutOfRange error on iPhone, but not on PC/Player 0 Answers

"Foreach" in an Array? 1 Answer

IndexOutOfRangeException: Array index is out of range. 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