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 asdf123 · Sep 07, 2013 at 05:55 PM · yieldweaponienumeratornewreturn

Wait For Seconds c#

i know that was asked a lot of times from other users, but i still can use wait for seconds on c#,

i have this:

 if(Input.GetKeyDown(KeyCode.Q)) {
     if(GetComponentInChildren<WeaponBase>().isAiming) {
          GetComponentInChildren<WeaponBase>().AimOut();
          yield return new WaitForSeconds(3.0f);
          SwitchWeapon();
     }
     else {
          SwitchWeapon();
     }
 }

i know that i need IEnumerator but i can't put it on the code. someone can help me ?

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 clunk47 · Sep 07, 2013 at 07:00 PM 1
Share

To call a Coroutine:

 StartCoroutine(YourCoroutine());
 
 //or use a string
 
 StartCoroutine("YourCoroutine");

You cannot call it by simply using:

 YourCoroutine();

That would be calling as if you had:

 void YourCoroutine(){}

But we are using IEnumerator here, docs I linked will help you better understand.

CLIC$$anonymous$$ HERE for more information on StartCoroutine

3 Replies

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

Answer by YoungDeveloper · Sep 07, 2013 at 06:22 PM

 bool canSwitch = false;
 bool waitActive = false; //so wait function wouldn't be called many times per frame
 
 IEnumerator Wait(){
     waitActive = true;
     yield return new WaitForSeconds (3.0f);
     canSwitch = true;
     waitActive = false;
 }
 
 
 if(Input.GetKeyDown(KeyCode.Q)) {
     if(GetComponentInChildren<WeaponBase>().isAiming) {
         GetComponentInChildren<WeaponBase>().AimOut();
         if(!waitActive){
             StartCoroutine(Wait());   
         }
         if(canSwitch){
             SwitchWeapon();
             canSwitch = false;
         }
     else {
         SwitchWeapon();
     }
 }
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 asdf123 · Sep 07, 2013 at 06:46 PM 0
Share

the swtichweapon() don't play

avatar image YoungDeveloper · Sep 07, 2013 at 07:01 PM 0
Share

right, i made i typo mistake, just as @clunk47 mentioned, I edited my answer

avatar image asdf123 · Sep 07, 2013 at 07:32 PM 0
Share

still not playing switchweapon()

avatar image
1

Answer by Deniz2014 · Mar 23, 2017 at 08:18 AM

I wanted to wait x Seconds in the Update() before continuing. So if someone, like me, searched this / needs this and is now here:

 private float timer = 0;
 
 private float timerMax = 0;
 
 void Update ()
 {
    text += "Allow yourself to see what you don’t allow yourself to see.";
 
    if(!Waited(3)) return;
 
    text += "\n\nPress any key!";
 }
 
 
     private bool Waited(float seconds)
     {
         timerMax = seconds;
 
         timer += Time.deltaTime;
 
         if (timer >= timerMax)
         {
             return true; //max reached - waited x - seconds
         }
 
         return false;
     }
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
avatar image
1

Answer by Deniz2014 · Mar 22, 2017 at 10:48 PM

I wanted to wait x Seconds in the Update() before continuing. So you cannot use the yield there (don't want to start a coroutine every frame right?)

So if someone, like me, searched this / needs this in and for the Update():

 private float timer = 0;
 
 private float timerMax = 0;
 
 void Update ()
 {
    text += "Allow yourself to see what you don’t allow yourself to see.";
 
    if(!Waited(3)) return;
 
    text += "\n\nPress any key!";
 }
 
 
 private bool Waited(float seconds)
 {
     timerMax = seconds;
 
     timer += Time.deltaTime;
 
     if (timer >= timerMax)
     {
         return true; //max reached - waited x - seconds
     }
 
     return false;
 }
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

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

Yielding in between functions... 2 Answers

Ienumerator return string 0 Answers

Corutine not behaving like it should? 1 Answer

How to use Coroutines in C++/CLI? 1 Answer

C# How to ping servers and get server latency? 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