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 junkrar · Mar 25, 2013 at 04:04 PM · c#waitforsecondsienumeratoryield waitforseconds

Proper Way to Wait in C#

I'm writing a game in C# and there's a few times where I have to wait a second or two for something. So I've been using yield return new WaitForSeconds(1.5f);

However, if I put this in my OnTriggerEnter function, I need to have it defined as IEnumerator instead of void. This isn't always acceptable, so I'm wondering if there's some other way to wait for a specified amount of time without having to change the return type on the function I'm writing it in.

EDIT: I've tried creating an extra function that does the waiting, but it just seems to not actually wait for the time I've specified. Here's the function:

     IEnumerator wait(float time)
     {
         yield return new WaitForSeconds(time);
     }

And here's how I've called it:

         changeColor(Color.red);
         loseHealth(1); //lose health
         canTakeDamage=false;
         StartCoroutine(wait (1.5f));
         Debug.Log ("done waiting");
         changeColor(Color.white);
         canTakeDamage=true;

The code is delaying when a player can be hit consecutively by an enemy. It also changes the colour to red when it can't be hit, and back to white when it can be hit. When I try it this way, the colour never actually changes, and the player can take damage again immediately (rather than 1.5 seconds later).

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by fafase · Mar 25, 2013 at 04:48 PM

"I need to have it defined as IEnumerator instead of void. This isn't always acceptable"

Would this fix it?

 void CallingFunction(){
    StartCoroutine(WaitingFunction());
 }
 
 IEnumerator WaitingFunction(){
     yield return new WaitForSeconds(1.5f);
 }

 void OnTriggerEnter(){
    CallingFunction();
 }

This is not really cute but you get the void function you seem to need.

Comment
Add comment · Show 5 · 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 junkrar · Mar 25, 2013 at 05:02 PM 0
Share

Thanks. Unfortunately though, I tried this (edited my question). I don't necessarily NEED the void function in this case, but say I'd like to call the waiting function from another function (just for neat code purposes). For example, I have a function called loseHealth(int health) and I want the character to be flash red then back to white. I have to put a small wait in there to accomplish the flashing. Why would I want this wait code to be written outside of the loseHealth function if I'm going to use it every time he loses health? That's just one instance, I was just wondering if there was a neater way to do it. Ideally, your method (and $$anonymous$$hada suggested it also) should work, but it doesn't seem to. $$anonymous$$aybe I'm doing something wrong?

avatar image deltamish · Mar 29, 2013 at 05:31 AM 0
Share

so you want the health to flash red and then back to white right

avatar image junkrar · Mar 31, 2013 at 06:28 AM 0
Share

yes that is exactly what I want. Or even flashing between red and white for a few seconds.

avatar image deltamish · Apr 02, 2013 at 03:39 PM 0
Share

hi so is the red texture applied to whole screen or just the player health bar and you want it to be red when the player is hit or not hit explain a bit more about the above

avatar image junkrar · Apr 03, 2013 at 07:29 PM 0
Share

When the player is hit, I want him to turn red for about a second and a half, then turn back to white. The waiting doesn't work if I use StartCoroutine, so I have to do the yield wait right in the OnTriggerEnter function. I'd rather do this in a different way.

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

12 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

Related Questions

IEnumerator's did not read "bool" after yield return new WaitForSeconds. 1 Answer

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

Loop with WaitForSeconds in IEnumerator appears to be incorrect by 10-15% 2 Answers

Multiple Cars not working 1 Answer

how to yield in C# 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