-1

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!

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • A "WaitForSeconds" class, which is a constructor, also suitable for delayed execution. public static IEnumerator DelayToInvoke(Action action, float delaySeconds) 8 { 9 yield return new WaitForSeconds(delaySeconds); 10 action(); 11 } – Housheng-MSFT Apr 20 '22 at 08:13
  • Please use the correct tags! Note that [`[unityscript]`](https://stackoverflow.com/tags/unityscript/info) is or better **was** a custom JavaScript flavor-like language used in early Unity versions and is **long deprecated** by now. – derHugo Apr 20 '22 at 14:05

1 Answers1

1

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/)
Giawa
  • 1,281
  • 1
  • 10
  • 21