How to break out update() immediately

Hi, I’ve been working on the xbox suspend issue. It requires the game to respond to suspend within 1 second. And now there is some heavy operations in some update() calls in some manager class, which is not easy to break down to coroutines.

I have a flag called ‘needSuspend’, and it’s value will be true when the game got the event to suspend. I need to call the XboxOnePLM.AmReadyToSuspendNow() to finish the suspend operation. In my test, it will effect after all scripts’ update(). I put the ‘if (needSuspend)’ check between the heavy operations codes. When the condition is true, is there any way to stop the current update() function of Monobehaviour immediately and abort all the code below to execute.

I have an idea to approach this effect, but it’s not good to cause an error…

 if (needSuspend)
            {
                XboxOnePLM.AmReadyToSuspendNow();
                int zero = 0;
                int forceBreakOut = 10 / zero;
            }

Throwing an exception is a very poor way to stop a function from executing. Just use a return statement: if (needSuspend) { XboxOnePLM.AmReadyToSuspendNow(); return; }

Ahh, okay. Umm... in that case, I'm afraid I can't offer more specific personal advice at the moment, then. Until I finish my current project, I'm still on Unity 2018, before URP and HDRP variants were implemented. At any rate, when an object does turn pink like that, it's the result of a shader either being broken or effectively broken (I assume URP limits functionality in shaders in a manner that the "Standard" shader no longer functions under it as-is).

1 Answer

1

Just call return. This will exit the method immediately, ignoring any following code.

So it would be:

if (needSuspend) {
    XboxOnePLM.AmReadyToSuspendNow();
    int zero = 0;
    int forceBreakOut = 10 / zero;
    return;
}

Thanks. But this only work or easy to use when the call stack is not deep. If I have a function which will be call by this sequence: Update()->InitBattle()->InitLoading()->InitPawns()->LoadPawns(). If I have this check in LoadPawns(), I might need to use return in all of these functions to actually stop all the execution in Update()...

This doesn't seem to work for me... My debug just keeps going and going once activated. Here's my code : private void Update() { if (brokenPiece1.activeSelf == false && brokenPiece2.activeSelf == false) { Debug.Log("Both plank pieces are disabled.... DO SOMETHING!"); return; } }