• 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 The-BOOM · Apr 16, 2010 at 02:21 AM · timeyielddelay

Delaying through yield

I have this code:

function Fire()
{
    yield WaitForSeconds(delay);
    Instantiate (bullet, transform.position, transform.rotation);
}

But the yield only performs once, and then they just rain bullets on me (which is not good). How can I make it so that it yields after it fires every time?

Now I'm having the problem that it crashes with this code:

function Start()
{
    while(true)
    {
        Fire();
    }
}

I don't get why it's doing this.

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

Answer by Eric5h5 · Apr 16, 2010 at 02:46 AM

That code does yield every time, but if you're calling it from Update, then a new instance of the function will be called every frame, resulting in many of them running simultaneously. So you either have to not use Update (which I would recommend), or use a different method instead of yield.

Also this has been asked before; see here.

Comment
Add comment · Show 2 · 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 The-BOOM · Apr 16, 2010 at 02:55 AM 0
Share

Where would I put it ins$$anonymous$$d of update?

avatar image Eric5h5 · Apr 16, 2010 at 03:35 AM 0
Share

@Sethbeastalan: See the code in the answer for the link I posted.

avatar image
0
Wiki

Answer by easy_da_man · Nov 05, 2012 at 01:34 AM

As to the "second" question: I do not know if it has been answered... but I guess it hasn't been answered in the link from Eric5h5. It is more specific to using while() in Unity methods... but Nubi and consorts might find this useful.

If you are in a while() slope with a condition that never changes, then you will stay in this loop forever. Unity then cannot call other functions because you are actually blocking the Unity engine in your while() loop.

Your

 function Start()
 {
     while(true)
     {
         Fire();
     }
 }

function has a condition that never changes -> true is always true. You might say that you are using yield and thus you are not blocking stuff. But Fire() yields back inside the while loop. And since true is still true you are firing again. You have no chance of escaping this while loop without changing your code.

Remember (at least for Unity): It is asynchronuous! That means that for "every step" your CPU takes Unity is updating its internal data. And then renders it. If you calculate the weather for the next two weeks in a while loop, no other functions are called during the time you are calculating the weather. And since this takes a lot of time Unity cannot recompute your nice game graphics and stuff and nothing will happen on screen.

Have a nice day and welcome to Jamaica

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 Eric5h5 · Nov 05, 2012 at 01:43 AM 0
Share

That wouldn't be an issue if the code was

 while(true)
 {
     yield Fire();
 }

Note that the question was substantially edited after I answered (should have asked a new question ins$$anonymous$$d), which is why my answer doesn't seem to match the question.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How to implement a delay between each time my gun fires (using coroutines or otherwise)?... 2 Answers

How to delay a function (how to use WaitForSeconds() and yield) 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do you delay Application.LoadLevel ? 2 Answers

How to change a Texture with a Delay 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