'Unity | how to make something happen after 10 seconds without delaying game [duplicate]
hello there I have been having trouble figuring out this problem. Basically I want to make something happen after 10 seconds without delaying the start function or slowing down the frame using the update function. I'm ort of new to unity so if there is anything that I would need to provide tell me. Thanks!
Solution 1:[1]
There are lots of ways! Here are a few examples:
- Use a Unity Coroutine (https://docs.unity3d.com/Manual/Coroutines.html)
void Start()
{
StartCoroutine(DoSomethingAfterTenSeconds());
}
IEnumerator DoSomethingAfterTenSeconds()
{
yield return new WaitForSeconds(10);
// now do something
}
- Use
FixedUpdateorUpdateto wait 10 seconds:
private float _delay = 10;
public void FixedUpdate()
{
if (_delay > 0)
{
_delay -= Time.fixedDeltaTime;
if (_delay <= 0)
{
// do something, it has been 10 seconds
}
}
}
- Use async/await instead of coroutines (https://forum.unity.com/threads/c-async-await-can-totally-replace-coroutine.1026571/)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Giawa |
