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 F-N · Nov 28, 2013 at 04:20 PM · startcoroutine

Problems with yield WaitForSeconds(); (IENumerator)

Hello, my prob is the next: I have a script that helps met to cut trees. I only got a problem with this. Because my IENumerator doesn't seem to be working. With the first tree my script works well. But if you immediately start with cutting down another tree after the first one, you cut it down in 0 seconds. And that is not what I want.... So please take a look at my script and explain me what I'm doing wrong.

if(isWoodCutting == true){//iswoodcutting gets true by means of another script isWoodCutting = false; StartCoroutine(WaitForTreeFall()); } //if a tree is cut down if(treeDown == true){ treeDown = false; Destroy(target); } } IEnumerator WaitForTreeFall(){ yield return new WaitForSeconds(10);//this sometimes seems to be ignored... if(target != null){ treeDown = true; } yield break; }

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 highpockets · Nov 28, 2013 at 04:37 PM 0
Share

I don't think this is your problem area. Are you sure isWoodCutting is not being set back to true by the other script before you start 'cutting'?

1 Reply

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

Answer by Minizinger · Nov 28, 2013 at 04:41 PM

Looks like the problem is: you use one boolean treeDown for whole script. So when you starting function second time it gets treeDown from 1st time. I'd better use something like this:

 bool isWoodCutting;
 bool cuttingTreeNow;
 float cooldown;
 void Update(){
 
 if(isWoodCutting && !cuttingTreeNow){
 cooldown = 10;
 cuttingTreeNow = true;
 }
 
 if(cooldown > 0)
 cooldown -= Time.deltaTime; //-1 per second
 
 if(cooldown <= 0 && cuttingTreeNow){
 cuttingTreeNow = false;
 Destroy(target);
 }
 }
Comment
Add comment · Show 3 · 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 Minizinger · Nov 28, 2013 at 04:44 PM 0
Share

Oh and don't forget to set isWoodCutting to false somewhere

avatar image F-N · Nov 28, 2013 at 05:34 PM 0
Share

Thank you for responding but I can't understand why my IENumerator is'nt working. I truly believe my booleans work fine. However your way will be working as well. :)

avatar image Minizinger · Nov 28, 2013 at 05:40 PM 0
Share

Atleast, ins$$anonymous$$d of using treeDown boolean, try to Destroy your target exactly in IEnumerator, this should work. But if you don't want allow player cutting 2,3,4 and more trees at same time, you better use cooldowns.

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

18 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Brain exploding ... Problem with an internal Coroutine manager 3 Answers

IEnumerator stops at yield 1 Answer

Is opening a Canvas possible in StartCoroutine()? 1 Answer

Can't Stop couroutines 1 Answer

Can I active some static function in ScriptableObject? 0 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