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!!!
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:
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.