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 Jerkenhag · Mar 09, 2013 at 05:04 PM · audiorandomyielddelayrepeat

How do I play a random sound each 5 seconds?

I now have this code:

 var sound1 : AudioClip;
 var sound2 : AudioClip;
 var sound3 : AudioClip;
 var sound4 : AudioClip;
 var sound5 : AudioClip;
 
 var playSound : int;
 
 function Start () {
 
     playSound = Random.Range(1, 5);
     
     while(playSound>0){
             
         if(playSound == 1){
         
         sound1.audio.Play();
         
         yield WaitForSeconds(5);
         
         }
         
         else if(playSound == 2){
         
         sound2.audio.Play();
         
         yield WaitForSeconds(5);
         
         }
         
         else if(playSound == 3){
         
         sound3.audio.Play();
         
         yield WaitForSeconds(5);
         
         }
         
         else if(playSound == 4){
         
         sound4.audio.Play();
         
         yield WaitForSeconds(5);
         
         }
         
         else if(playSound == 5){
         
         sound5.audio.Play();
         
         yield WaitForSeconds(5);
         
         }
     
     }
     
 }

But I get an error on every line with audio.Play(); with text about Boo when I'm writing in JS. Also no sound comes.

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 fafase · Mar 09, 2013 at 05:34 PM 0
Share

As a side note, avoid while loop as you are doing in a Start function.

 playSound = Random.Range(1, 5);
  
 while(playSound>0)

playSound will always (as in forever) be greater than 0 since it is between 1 and 5. As a result, the script will never (as in always) leave the start function.

As a rule of thumb, do not use while loop of this kind in Update or Start (or any function actually). You cannot control how long it will take for the sentinel to be false and leave the loop.

You may ins$$anonymous$$d start a coroutine which will run until the condition is met without blocking the rest of the game.

You could check this http://unitygems.com/coroutines/ for more info on coroutine and their usage.

avatar image Jerkenhag · Mar 09, 2013 at 05:43 PM 0
Share

Alright, thank you. I have used while loop in other scripts since I thought it was the easiest way to have something go on forever but I will keep that in $$anonymous$$d!

avatar image fafase · Mar 09, 2013 at 05:54 PM 0
Share

It is fine as long as you control how many round it will be performed. Example:

 var gos :GameObject[] =GameObject.FindObjectsWithTag("Enemy");
 while(gos.$$anonymous$$oveNext()){
    //do Stuffs
 }

$$anonymous$$oveNext will return false when getting out of range and gos is of limited amount so we are sure to get to the end at some point. This one is fine in a function.

But in your way, random will never return and even though you would have:

 var num = Random.Range(0,5);
 while(num>0)

You cannot know how many round the compiler will take before returning 0, maybe it will take 10s. (let's not get into probability shall we)

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by fafase · Mar 09, 2013 at 05:21 PM

 var sound : AudioClip[] = new AudioClip[5];

 function Start(){
     InvokeRepeating("PlayClipAndChange",0.01f,5.0f);
 }
 
 function PlayClipAndChange(){
     audio.clip = sound[Random.Range(0, 5)];
     audio.Play();
 }

To be tried but that should do it. You have an array in which you drag and drop your audio clips. Then you start InvokeRepeating which calls the function each 5 sec. Then, in the function, the sound is assigned using a random value and played.

Comment
Add comment · Show 5 · 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 Kiloblargh · Mar 09, 2013 at 05:29 PM 0
Share

Your answer is righter. I didn't even notice that the whole thing was in Start()- of course it's not going to play every 5 seconds without an InvokeRepeating(). Going to go pour myself a new cup of coffee.

avatar image Jerkenhag · Mar 09, 2013 at 05:59 PM 0
Share

Thank you, that worked! Although I must ask, why use [Random.Range(0, 6)] since it only contains 5 clips? I changed it to 4 ins$$anonymous$$d of 6 and it worked but is there anything I may have done wrong by that?

avatar image Kiloblargh · Mar 09, 2013 at 06:09 PM 1
Share

Actually you need to use sound[Random.Range(0,5)], because arrays start at 0, (0,6) will sometimes return sound[5] which is actually the nonexistant 6th sound and throw an IndexOutOfRangeException.

But Random.Range to get an int will never return the top number. So you have to put it one more than one less than the real number you have to choose from, which is... 5. $$anonymous$$akes perfect sense, right?

avatar image fafase · Mar 09, 2013 at 06:22 PM 0
Share

Oops yep right, the array is 5 so the max value is 4 then Range is (0,5) This is because when using int max value is excluded, if using float max value is included. Don't ask why...

avatar image Jerkenhag · Mar 09, 2013 at 06:45 PM 0
Share

Alright then I get it, I knew about that it started at 0 but didn't know it didn't go to the highest.

Edit: And also to answer fafase above I have used the exact same style which I had here, so it was impossible for it to ever stop.

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

11 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

Related Questions

Random Audioclip. No Repeat ?? 1 Answer

Play a short audio file, then random delay, then repeat. 1 Answer

How to Delay A Ball shooting script 1 Answer

How do you delay Application.LoadLevel ? 2 Answers

Random footsteps 5 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