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!
CodePudding user response:
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
FixedUpdate
orUpdate
to 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/)