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 haruhi4 · Jul 06, 2016 at 04:56 PM · unity 5soundmusic

Music player giving indexoutofbounds error

I'm developing a game that uses the music player from http://answers.unity3d.com/questions/652919/music-player-get-songs-from-directory.html (the best answer in c# it's what i'm using)

It's great, but in my app, i don't have a play button on the gui. I want the music to start right away after the "ReloadSounds()" function ends. So, I call the "PlayCurrent();" at the last line of the "ReloadSounds()" function.

The problem is sometimes i get a indexoutofbounds error and it's because the List clips attribute is empty when i call playcurrent()

Strangely, there's no problem if i keep the calls to print(); on the function LoadFile(string path) (there are 2 prints on this function, like the example on the link).

I think the error is cause because i start coroutines and i think coroutines in unity are like threads, but people say they're not and that i shouldn't worry about thread-safety when i use them.

Is there any way to make my app work without relying on print() calls on the LoadFile(string path) function? Is coroutine a thread on unity?

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
Best Answer

Answer by Eno-Khaon · Jul 06, 2016 at 05:52 PM

In that script, the files are specifically being loaded using a coroutine in order to avoid any major performance impact, should the file loading process take longer than expected.

What you're currently facing is effectively a race condition. Will the file be done loading first or will you try to play it first?

Fortunately, there's a relatively easy way to deal with this:

First, rather than trying to play the audio immediately after starting the coroutine StartCoroutine(LoadFile(s.FullName)), you can do something like check whether the List<> is currently empty before adding the file at the end of LoadFile() and, if so, begin playing it. Or, alternatively, you could simply check whether the audiosource is already playing anything first.

Again, either of these options would occur at the end of LoadFile() rather than at the end of ReloadSounds() because there's no guarantee when using the coroutine that the file will actually be ready in time.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Is it possible to modify the Audio of a complete Unity project downloaded from the Asset Store? 1 Answer

Debug Editor crashes (Possible sound related ???) 3 Answers

.wav file not working 2 Answers

audio change object size 1 Answer

Audio Scripting 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