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 Jake_002 · Aug 21, 2012 at 08:02 PM · javascriptcoroutinetiming

Problems in a footstep coroutine

Hello! I wrote a simple script that plays a random sound from an array if you have one of the arrow keys pressed down. This is supposed to simulate footsteps. The problem is that sometimes the sound for the footstep won't play. This seems to happen at completely random times. Take a look at the code and then I'll explain more.

 var grassOne : AudioSource;
 
 var grassTwo : AudioSource;
 
 var grassThree : AudioSource;
 
 var grassFour : AudioSource;
 
 var walkSounds = new Array ();
 
 var soundToPlay : int;
 
  
 
 function Start(){
 
     walkSounds[0] = grassOne;
 
     walkSounds[1] = grassTwo;
 
     walkSounds[2] = grassThree;
 
     walkSounds[3] = grassFour;
 
     StartCoroutine("Loop");
 
 }
 
 function Loop(){
 
     while(true){
 
         yield WalkSoundPlay();
 
     }
 
 }
 
 function WalkSoundPlay(){
 
     while(Input.GetButton("Horizontal") || Input.GetButton("Vertical")){
 
         soundToPlay = Random.Range(0, 4);
 
         walkSounds[soundToPlay].Play();
 
         yield WaitForSeconds(.5);
 
     }
 
 }

Yesterday I figured out how to use the StartCoroutine command and a loop function to execute another function at predetermined time intervals. It worked perfectly in my applyDamage code. Here's how I understand this code should work. When the script is loaded, it sets the first four values of an array to be four different AudioSources. It also starts the coroutine called "Loop". (Question: Using the StartCoroutine keyword executes the code in the specified function. I understand that if you called "Loop();" in the Start function it would only execute once, but what's the difference between using StartCoroutine in the Start function and calling "Loop();" in the Update function?) Then, the while loop repeatedly executes the WalkSoundPlay function. (Question: I'm assuming that the while loop checks if it's conditions are true every frame. Is this true?) Every frame(?), the WalkSoundPlay function checks if your arrow keys are held down. If they are it sets soundToPlay to a random number between 0 and 3 (because when you use Random.Range with an integer, the max number is exclusive but the min number is inclusive right?). It then accesses the value in the walkSounds array that matches the number we just generated and since that value is an AudioSource, it plays it. Finally it tells the script that it has to wait .5 seconds before it can execute again. This is from the viewpoint of a 14 year old who has only been programming for a couple of months. The script looks really simple but I can't figure out what is causing the problem. here's what I have tried already:

Decreasing the WaitForSeconds time to .3

Increasing the WaitForSeconds time to 1

Making a new boolean called playSounds. In the update function, I wrote: If the arrow keys are held down set playSounds to true, else set it to false. Then under the WalkSoundPlay while loop's conditions instead of what's there now I wrote "playSounds == true". This had no effect on the script but I deleted it because I didn't want to use the update function if i didn't have to.

Getting rid of the Loop function and any references to it, then calling "WalkSoundPlay();" in the update function. This is what I tried to do before I knew about the StartCoroutine command. It still doesn't work.

If anybody knows a fix for this, that would be fantastic. Thank you for taking the time to read my post!

Comment
Add comment · Show 1
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 Mander · Aug 21, 2012 at 08:42 PM 0
Share

i can't see y that isnt working but since u said something about random times. maybe it has to be with ur random, maybe for some weird reason it just doesn't play if u get the same number in ur random soo u could try this. so u won't get the same number in row

if (soundToPlay == soundToPlay && soundToPlay != 3) { soundToPlay+=1; }else{ soundToPlay = 0; }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mander · Aug 21, 2012 at 08:43 PM

i can't see y that isnt working but since u said something about random times. maybe it has to be with ur random, maybe for some weird reason it just doesn't play if u get the same number in ur random soo u could try this. so u won't get the same number in row. place this after this line `soundToPlay = Random.Range(0, 4);`

 if (soundToPlay == soundToPlay&& soundToPlay != 3)
 {
   soundToPlay+=1;
 }else{
 soundToPlay = 0;
 }
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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

changing a coroutine's argument at the coroutine's runtime? 1 Answer

Coroutine gets skipped in javascript/unityscript 0 Answers

Stop coroutine not working 1 Answer

Execute code for a certain amount of time 1 Answer

Keeping track of time? 4 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