Home > Enterprise >  Unity | how to make something happen after 10 seconds without delaying game
Unity | how to make something happen after 10 seconds without delaying game

Time:04-16

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:

  1. 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
    }
  1. Use FixedUpdate or Update 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
            }
        }
    }
  1. Use async/await instead of coroutines (https://forum.unity.com/threads/c-async-await-can-totally-replace-coroutine.1026571/)
  • Related