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
2
Question by Bokaii · Aug 11, 2014 at 04:45 PM · multiplayerfpsparkour

yield WaitForSeconds c#

How does yield WaitForSeconds work in c#?

i've tried, but I can't seem to make it work, and I can't find anything that helps me online!

I'm making a game, where when I look at a certain object, and the distance between me and the object is less than 2 I wan't to do something. But only after, like 5 seconds. So I need the WaitForSeconds function. Please help me!!!

Comment
Add comment · Show 2
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 Bokaii · Aug 11, 2014 at 03:45 PM 0
Share

Please explain :)

avatar image Bokaii · Aug 11, 2014 at 06:53 PM 0
Share

When I use invoke, it calls the function inside it, for example print("SO$$anonymous$$ETHING");, like 17 times!

4 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by gjf · Aug 11, 2014 at 04:47 PM

 Invoke("functionName", 5.0f);

will invoke a function called functionName after 5 seconds...

Comment
Add comment · Show 4 · 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 Bokaii · Aug 11, 2014 at 04:56 PM 1
Share

Thank you, I will try that!

avatar image _Shockwave · Apr 20, 2015 at 07:14 AM 0
Share

using Invoke() is so much better than waiting and coroutines. It also works consistently the same way in JS and C#. I found about this from somewhere else but some stumbled upon this again and wanted to say thank you :)

avatar image Doza_3D · Dec 04, 2016 at 05:59 AM 0
Share

gjf, This is a couple years old but thank you very much! I prefer your solution over using a coroutine. Thank you very much!

avatar image Hosomi · Feb 10, 2018 at 08:30 PM 0
Share

Invoke doesn't work if the script is not a monobehaviour

avatar image
4

Answer by Wuzseen · Aug 11, 2014 at 05:01 PM

As gjf stated, you could use invoke.

yield waitforseconds, however, is something you use inside a coroutine.

A coroutine is a block of code that executes on its own time frame. IE. not in Update/FixedUpdate/Etc.

 IEnumerator ExecuteAfterTime(float time) {
     
     // stuff
     yield return new WaitForSeconds(time); // the program waits time seconds before continuing
    // more stuff
 }

A coroutine is often used to make code that "blocks" while waiting for a condition to be true. Another good example is a spawning system where you want an item to spawn continuously, every X seconds:

 IEnumerator SpawnPrefabContinuously() {
     while(true) {
         yield return new WaitForSeconds(spawnTime);
         SpawnPrefab();
     }
 }

Another common yield statement is: yield return null doing this causes the coroutine to pause a single frame. You can then make alternate update blocks using this method. Some people never use update and just use a coroutine (though this isn't always proper either).

It lets you separate time dependent code into separate code chunks instead of having a lot of branching logic inside update.

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 BillyForce · Aug 11, 2014 at 05:45 PM

In c# you have to use an IEnumerator. Google for it ;) or http://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html

 void Event ()
 {
     StartCoroutine("DoSomething");
 }
 
 
 IEnumerator DoSomething()
 {
    yield return new WaitForSeconds(5f);
    print("Ok");
 }
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 Hosomi · Feb 10, 2018 at 08:40 PM 0
Share

This only works in a monobehaviour

avatar image
1

Answer by ulysses 31 · Aug 11, 2014 at 05:45 PM

Call this outside of Start() or Update()...

 IEnumerator wait()
 {
    //Do whatever you need done here before waiting
 
    yield return new WaitForSeconds (2f);
 
    //do stuff after the 2 seconds
 }


Call this in Update()...

 StartCoroutine("wait");
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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

unity3d invoke 0 Answers

Add one on local z axis 3 Answers

[CLOSED]RaycastAll Help 1 Answer

Photon enable/ disable object 2 Answers

Check if RaycastAll hits[i].point == null 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