Hi everybody!
currently, to wait in scripts, i make a float variable that augments every frame, and when it reaches a threshhold, the script continues on. so if the frames per second of the game is 30:
public float WaitTime = 0.0f;
public float Threshhold = 90f;
WaitTime++;
The WaitTime will be of 3 seconds before it reaches the threshhold. I am sure this has a huge hit on performance if you have hundreds of scripts using this technique.
I researched on the IEnumerator and YieldForSeconds thing, but cannot seem to comprehend it. Is there any simple way to wait an amount of time in Cscripts or can somone explain in detail IEenumerator and YieldForSecond functions for a noob? thank you very much 
if anyone is still unsure of what I am asking, I want to know how to make my script wait a number of seconds before passing to the next line of code. Thanks!
3 Answers
3
Coroutines and Invokes are very simple to use, actually. I suggest you spend some time acquainting yourself with them. All of your projects will benefit from their use.
For instance, if you wanted something to repeat every 3 seconds you could use either a Coroutine or an Invoke.
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
public bool useInvoke;
public bool running;
void Start()
{
if (useInvoke)
InvokeRepeating("TestInvoke", 0, 3);
else
StartCoroutine(TestCoroutine());
}
void TestInvoke()
{
Debug.Log("TestInvoke()");
}
IEnumerator TestCoroutine()
{
running = true;
while (running)
{
Debug.Log("TestCoroutine()");
yield return new WaitForSeconds(3);
}
}
}
Attach that script to a gameObject and play with the useInvoke flag from the inspector if you want. Unchecking the running box in the inspector will quit the coroutine. Hopefully you can use this as a template to implement your own! Good luck!
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 += "
Press any key!";
}
private bool Waited(float seconds)
{
timerMax = seconds;
timer += Time.deltaTime;
if (timer >= timerMax)
{
return true; //max reached - waited x - seconds
}
return false;
}
what about when the code is not in a void? for example:
GameObject muzzleflash = instantiate…
yield WaitForSeconds (0.4f);
Destroy (muzzleflash);
this code above don’t work. how could I do lines of code that simple without create a new void?
I tried to use IEnumerator like this;
GameObject muzzleflash = instantiate…
Wait (0.4f);
Destroy (muzzleflash);
IEnumerator Wait (float amount) {
yield return new WaitForSeconds (amount);
}
please tell me what I am doing wrong.
Thanks, but suppose does this work for: suppose i have a script wich does: somevar = 1; "Wait 3 seconds"; somevar = 3 somevar will be 1, then after 3 seconds change to 3. How would i go about achieving this? calling the functions you mentionned above in the script? a bit like: somevar = 1; test.testinvoke(); somevar = 3; or somevar = 1; test.TestCoroutine(); somevar = 3; ? thanks for quick answer!
– RedmothYes. If you want to wait to set a variable then you simply set it after the delay. private int valueToSetTo; void Start() { valueToSetTo = 5; Invoke("SetVariable", 3); } void SetVariable() { someVar = valueToSetTo; }
– iwaldropAnd please mark this question as answered. I'll still help you along, but we might as well get it off of the unanswered list. :)
– iwaldropThank you very much :)! I like that Invoke trick. Very usefull and simple indeed, I didn't even know such a function existed! That should do the trick, thanks for the answer!
– RedmothIf you are going to post an answer to your own question, please put the full details of the answer in the post.
– Jeff-Kesselman