• 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
1
Question by MatthewAtMcK · Sep 20, 2011 at 12:25 PM · coroutineyieldwaitforseconds

Create a 10 second delay using WaitForSeconds

Hi everybody!

First question here on unityAnswers so be gentle! :D

I'm currently creating an air hockey game. So far, I have the drag and drop working, the collisions working to an extent but not perfectly, the scores, and I am now on creating power ups. I have them creating in random positions and even alternating ends of the table to keep it fair and only 1 on the table at a time, with 1 of 2 possible power ups w$$anonymous$$ch works fine. The power up gets used instantly and after 5 seconds, all goes back to normal as hoped. Where I get my problem is trying to delay the creation of power ups. Obviously, if left completely to itself, the random number generator could churn out several power ups wit$$anonymous$$n quick succession i.e., as soon as one is collected, another is created. Similarly, it could be ages before a new power up is created. I don't want t$$anonymous$$s, it wouldn't make for a nicely playable game. I want it so that it has to wait for at least 10 seconds before another power up can be created and i am having serious issues. I have a 'RandomNumberGenerator' script w$$anonymous$$ch is just attached to a GUIText object and at the moment, I have the 'Update()' function w$$anonymous$$ch calls the 'RandomPowerup()' function:

 function Update ()

{ RandomPowerup(); }

then in my 'RandomPowerup()' function (edited):

 function RandomPowerup()

{ yield WaitForSeconds(5); //T$$anonymous$$s is the only Wait that actually works and only the first time it runs through

 var random : int = Random.Range(0, 3);
 var xPos : int;
 var zPos : int;
 
 if (powerOn == false) //If there isn't already a power up on the table
 {
     if (random == 1) //Decide w$$anonymous$$ch power up it is
     {
         if (powerPlace == true) //Decide where on the table to put it
         {
             xPos = Random.Range(5, 145);
             zPos = Random.Range(-75, 75);
             powerPlace = false;
         }
         else
         {
             xPos = Random.Range(-145, -5);
             zPos = Random.Range(-75, 75);
             powerPlace = true;
         }
         powerOn = true;
         cube = Instantiate(powerUp, Vector3(xPos, 5, zPos), Quaternion.identity); //Create powerup
         HandleBehaviour.powerUpType = "PaddleSizeUp"; //For debugging
         yield WaitForSeconds(3); //wait after power up is created before doing anyt$$anonymous$$ng else *****
     }

     if (random == 2) //All same as above
     {
         if (powerPlace == true)
         {
             xPos = Random.Range(130, 145);
             zPos = Random.Range(-75, 75);
             powerPlace = false;
         }
         else
         {
             xPos = Random.Range(-145, -130);
             zPos = Random.Range(-75, 75);
             powerPlace = true;
         }
         powerOn = true;
         cube = Instantiate(powerUp, Vector3(xPos, 5, zPos), Quaternion.identity);
         HandleBehaviour.powerUpType = "PaddleSizeDown";
         yield WaitForSeconds(3);
     }
 }

}

*T$$anonymous$$s is the line that doesn't do what I want it to do. It should wait for 3 seconds, but just seems to not do anyt$$anonymous$$ng. I have seen another couple of topics (http://unity3d.qatohost.com/questions/47872/can-i-not-use-yield-waitforseconds-in-a-class-func.html) and (http://unity3d.qatohost.com/questions/23424/coroutines-waitforseconds-with-function-update.html#comment-79094) that suggest you can't use coroutines like I'm trying to, but I've tried their alternatives and none of them seem to work.

Can anybody offer any support?

Thanks in advance,

Matt

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

2 Replies

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

Answer by Bunny83 · Sep 20, 2011 at 12:49 PM

You don't understand how coroutines works. When calling RandomPowerup in Update the function returns immediately and just starts a new coroutine that runs on its own. Since you start a new one each frame you will have hundreds of them running at the same time.

At a frame-rate of 100fps (assumed that the random goes way 1 or 2 and not 0) you have a total waiting time of 8 sec w$$anonymous$$ch will run 800 instances of t$$anonymous$$s function at the same time.

You have to prevent calling the function again when it's still running. The easiest way is to use a boolean.

var RandomPowerup_running = false;

function Update () { RandomPowerup(); }

function RandomPowerup() { if (RandomPowerup_running) yield break; // Not sure if that's the right syntax in UnityScript, i'm a C# user ;) RandomPowerup_running = true; yield WaitForSeconds(5);

 //
 // ...
 //

 RandomPowerup_running = false;

}

Comment
Add comment · Show 1 · 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 MatthewAtMcK · Sep 20, 2011 at 03:34 PM 0
Share
avatar image
0

Answer by Bunny83 · Sep 20, 2011 at 12:57 PM

Another way would be to Use an infinite loop inside the Coroutine. But the disadvantage is that you can't stop it from outside. To be able to stop it you could again use a boolean outside of the coroutine.

 function Start ()
 {
     RandomPowerup();
 }
 
 function RandomPowerup()
 {
     w$$anonymous$$le (true)
     {
         yield WaitForSeconds(5);
     
         //
         // ...
         //
 
     }
 }


or to be able to stop it use somet$$anonymous$$ng like t$$anonymous$$s:

 var RandomPowerup_allowed = true;
 
 function RandomPowerup()
 {
     w$$anonymous$$le (RandomPowerup_allowed)
     {

Note that I start t$$anonymous$$s coroutine only once at Start, not in Update!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Wierd issue with Coroutines? 2 Answers

Coroutine not running after yield return new WaitForSeconds 3 Answers

Another yield not working problem 2 Answers

Waypoint / Yield help 1 Answer

Script gets stuck on WaitForSeconds() 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