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
1
Question by Lanipoop · Oct 26, 2010 at 01:37 AM · yieldwaitforsecondsswitchstatement

Yield statement is not working

I'm trying to make a speed pickup that only lasts for 3 seconds, but right now the speed boost is lasting forever. Here is my code:

public enum PickupType { Health = 0, Speed = 1 }; public PickupType pickupType = PickupType.Health;

bool ApplyPickup (PlayerScript playerStatus) { print("ApplyPickup() called"); switch (pickupType) { case PickupType.Health: healthPickup(); break;

         case PickupType.Speed:
         print("Speed pickup found");
         PlayerScript.moveSpeed += 50000;
         StartCoroutine("speedPickup");
         break;
     }

     return true;
 }

 public IEnumerator speedPickup ()
 {
     print("speedPickup() running"); 
     yield return new WaitForSeconds(3);
     print("3 seconds passed");
     PlayerScript.moveSpeed = PlayerScript.originalSpeed;
     print("moveSpeed reverted to original");
 }

void OnTriggerEnter (Collider col) { if (!ApplyPickup (playerStatus)) return; }

The code runs just fine, with no errors, but the last two messages, "3 seconds passed" and "moveSpeed reverted to original" are the only ones that don't appear. What's wrong with my code? :/ Does it have anything to do with being in a Switch statement?

Comment
Add comment · Show 6
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 jrfactotum · Oct 26, 2010 at 02:21 AM 0
Share

Where are you calling ApplyPickup()?

avatar image Lanipoop · Oct 26, 2010 at 02:29 AM 0
Share

OnTriggerEnter. I edited my original post to include where ApplyPickup() is applied.

avatar image jrfactotum · Oct 26, 2010 at 02:33 AM 0
Share

What is the variable pickupType? Where is its value set?

avatar image Lanipoop · Oct 26, 2010 at 03:16 AM 0
Share

Edited the original again :)

avatar image jrfactotum · Oct 26, 2010 at 01:04 PM 0
Share

Is pickupType being changed to PickupType.Speed somewhere, and you're sure "speedPickup() running" is shown?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by lampshade · Oct 26, 2010 at 05:16 AM

Try to implement something like this into your code:

private bool isRunning = false;

public IEnumerator MyEnumFunc() { isRunning = true; // Running

for (int i = 0; i < 30 i++) {

       isRunnning = false; // Stopped or something
       yield return new WaitForSeconds(0.1f); // Wait for 3 seconds!

}

isRunning = true; // Running again

}

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

No one has followed this question yet.

Related Questions

Understanding yield inside a for or a while loop 2 Answers

About removing the backlight of a button after a set of time 2 Answers

Why Is yield return new WaitForSeconds() not working 2 Answers

Some yield mess 2 Answers

yield WaitForSeconds() causing compiler errors 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