Why is unity OnApplicationPause called twice on android?

4.5k views Asked by At

I have a simple game object (the main UI object) with OnApplicationPause that prints out the Pause status (true/false) in the debug log. When I run it on android, and press the Home Button to leave the app, I see that the OnApplicationPause() is called twice in a row, with the same pauseStatus (true). The same happens when I reopen the app: the OnApplicationPause is called twice with Pause status false.

Why is it being called twice and can I avoid it? Am I doing anything wrong?

I also tried to create a separate GameObject, that includes the same debug code. The issue doesn't occur there - only in my main UI object.

1

There are 1 answers

1
jlevet On BEST ANSWER

Are you 100% sure that your script containing the OnApplicationPause() method is not present twice in the Scene when Pause occurs? or present on a previous Scene on a DontDestroyOnLoad GameObject?

OnApplicationPause is called on each active MonoBehaviour when pause occurs.

So having two Debug.Log means two scripts instances running if you don't call it manually yourself.