- Home /
The question is answered, right answer was accepted
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.
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!
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.
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.
Couldn't you just make an array of the textures and initialize them through the IEnumerator?
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.
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
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.
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
johnnlambada , thakx , it really works , keep it up :)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
What is wrong with this use of WaitForSeconds? 1 Answer
Yield WaitForSec help in C sharp 1 Answer