Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Jan 27, 2015 at 02:44 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
13
Question by helloiam · Nov 19, 2012 at 06:56 PM · c#yieldwaitforsecondsexplanation

C# yield waitforseconds

How to set the yield waitforseconds in Unity3D C#? I looked for tutorials and stuff, but I still couldn't figure it out. Now I am making a game and I want to use it, so anybody that can help me out with a good explanation, please no websites, just a clean explanation by yourself.

Kind regards,

helloiam.

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

3 Replies

  • Sort: 
avatar image
53
Best Answer

Answer by Yokimato · Nov 19, 2012 at 07:18 PM

Wait for seconds requires a couple things in order to work properly and chances are if it's not working you're probably just missing on of the requirements. Here's an example:

 IEnumerator MyMethod() {
  Debug.Log("Before Waiting 2 seconds");
  yield return new WaitForSeconds(2);
  Debug.Log("After Waiting 2 Seconds");
 }

Simple right? The only trick here is to make sure the return type of the method using the `WaitForSeconds` call is `IEnumerator`. Okay, so now we're half done but the next thing is to call this `MyMethod` and that looks like:

 StartCoroutine(MyMethod());

And there's the 2nd trick. You need to call the method as a coroutine otherwise you're not going to have it work.

Now that you get how it works, jump back to the yield statement. Anything you want to happen after the wait goes after the yield statement and everything you want to happen before, goes before the yield statement. Hope this helps!

Comment
Add comment · Show 13 · 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 fafase · Nov 19, 2012 at 07:32 PM 1
Share

I think but am not sure that you are missing the () in the function call.

 StartCoroutine($$anonymous$$y$$anonymous$$ethod);->StartCoroutine($$anonymous$$y$$anonymous$$ethod());

or

 StartCoroutine("$$anonymous$$y$$anonymous$$ethod");

I could be wrong though.

avatar image Yokimato · Nov 19, 2012 at 07:36 PM 0
Share

@fafase, Yep! Fixed, thanks.

avatar image helloiam · Nov 20, 2012 at 06:53 AM 0
Share

I know this method now, really thanks a bunch! I do now have it, but I use textures and by 10 textures I want to use that yield wait for seconds. How do I do that? Because it don't work with one IEnumerator, I must have to make 10 different numeratores, then it works but otherwise it wont.

avatar image energyred7 helloiam · Oct 05, 2015 at 11:22 AM 0
Share

Couldn't you just make an array of the textures and initialize them through the IEnumerator?

avatar image fafase · Nov 20, 2012 at 07:17 AM 0
Share

You want to swap 10 textures is that it?

avatar image helloiam · Nov 20, 2012 at 06:20 PM 0
Share

When I pickup a item it shows my texture but I have a lot of textures and I want them to use the yield wait for seconds calling it one class ones, it only works when I do this for every single texture, so 10 textures 10 different ienumerators and I don't want that, it takes memory. So basically 10 textures that uses the same iemumerator class and not 10 ienumeratores (namestored) for 10 textures. Sorry my English btw.

Show more comments
avatar image
16

Answer by RetepTrun · Nov 19, 2012 at 07:33 PM

Or..

Avoid the problem altogether and use Invoke http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.Invoke.html

Comment
Add comment · Show 2 · 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 Yokimato · Nov 19, 2012 at 07:38 PM 0
Share

Not always the case as shown here: http://answers.unity3d.com/questions/8759/is-invoke-just-a-short-version-for-yield-waitforse.html

But for most, I would agree.

avatar image twright151 · Mar 04, 2013 at 01:45 AM 1
Share

This solved my problem, would upvote if I could.

avatar image
5

Answer by johnnylambada · Sep 17, 2013 at 05:33 PM

From my StackOverflow answer:

This:

 using UnityEngine;
 using System.Collections;
 public class MainCamera: MonoBehaviour {
   void Start () {
     Debug.Log ("About to StartCoroutine");
     StartCoroutine(TestCoroutine());
     Debug.Log ("Back from StartCoroutine");
   }
   IEnumerator TestCoroutine(){
     Debug.Log ("about to yield return WaitForSeconds(1)");
     yield return new WaitForSeconds(1);
     Debug.Log ("Just waited 1 second");
     yield return new WaitForSeconds(1);
     Debug.Log ("Just waited another second");
     yield break;
     Debug.Log ("You'll never see this"); // produces a dead code warning
   }
 }

Produces this output:

 About to StartCoroutine
 about to yield return WaitForSeconds(1)
 Back from StartCoroutine
 Just waited 1 second
 Just waited another second


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 ZeeshanAkhtar_UOG · Sep 05, 2014 at 12:35 PM 0
Share

johnnlambada , thakx , it really works , keep it up :)

Follow this Question

Answers Answers and Comments

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

Related Questions

Multiple Cars not working 1 Answer

Mysteries of yield 1 Answer

WaitForSeconds not running 2 Answers

How can I make the health points constantly go down? 1 Answer

Only the first half of my coroutine works 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