Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

About InvokeRepeating

Discussion in 'Scripting' started by wwu39, Apr 7, 2020.

  1. wwu39

    wwu39

    Joined:
    Nov 21, 2018
    Posts:
    25
    I have a function called by InvokeRepeating every 1/60 second.
    If I call CancelInvoke at sometime, would that function gets killed in the middle of execution, i.e. without returning, or it gets killed only after it returns.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    From the standpoint of user-level scripts, Unity is single-threaded so all functions will finish and return before anything else scripting-wise happens. Functions will never be interrupted midway through unless they are actually IEnumerators and execute a yield return null;, or obviously if they throw an exception or return.

    If you are using InvokeRepeating 60 times a second you might just as well use Update(), as it will have virtually the same effect and be far more understandable and simple.
     
  3. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    I'd even go further and say a 60 times/second repeating invoke performs worse than Update because it burns a lot cycles for the invoke at the end of the frame. Plus, and this may not be clear to everyone, InvokeRepeating is in no way guaranted to be invoked as often, or quickly as the time interval specified -- using Invokerepeating to try to force 60 fps does not work if your Frame cycle takes longer than 1/60 of a second..
     
    Kurt-Dekker likes this.
  4. wwu39

    wwu39

    Joined:
    Nov 21, 2018
    Posts:
    25
    Thank you guys for both your answer and valuable advice. So for high frequency events I'd better use Update. For low frequency events like 10 times per second, is InvokeRepeating better performance-wise? What's the dividing line?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Don't speculate about performance. Implement what you need in the most straightforward way possible, ideally using as little code as humanly possible. Code that doesn't exist cannot have bugs.

    If it is fast enough, you are done, and your codebase is simple and small and easy to read by you and anyone else.

    If it turns out not fast enough, ONLY then should you consider performance.

    However, you should NOT speculate about what might be slowing it down.

    You should NOT ask others about what is slowing your code down.

    You SHOULD attach the profiler and get hard answers out of that, or else do your own A/B instrumentation, identifying where things are slowing down, and then perhaps come back with pointed questions about what your profiling and instrumentation reveals.

    Finally, always keep in mind how Unity's lifecycle works. It's all in this graph:

    https://docs.unity3d.com/Manual/ExecutionOrder.html
     
    OTO1 likes this.