OnApplicationPause is been called before Awake

Hi,

This is what documentation states:

OnApplicationPause: This is called at the end of the frame where the pause is detected, effectively between the normal frame updates. One extra frame will be issued after OnApplicationPause is called to allow the game to show graphics that indicate the paused state.

Awake: This function is always called before any Start functions and also just after a prefab is instantiated. (If a GameObject is in-active during start up Awake is not called until it is made active, or a function in any script attached to it is called.)

Doesn’t this mean that all awakes should be called before anything else and when objects are instantiated the system would call OnApplicationPause on startup?

I’m really confused about this behaviour.

Thanks in advance.

1 Like

OnApplicatonPause occurs when the application is suspended/resumed, it has no relation to instantiating or activating (in-active) objects or startup functions.

2 Likes

Hi,

Ok, this make sense but for a method behaviour to be called first a game object must be in the scene using it. So, an instance of the object containing the behaviour would have been instantiated previously and awake is called right after the object is instantiated. On the other hand unity is not multithreaded, so, how is it possible that I receive OnApplicationPause when awake has not been called?.

This thoughts can be a bit naive, but it is what I understand from all I read and your answer. I would really apreciate that someone could clarify this.

Thanks in advance.

OnApplicationPause is a system-level event, so it’s always broadcast when the application is “paused”. Whether anyone is listening (i.e. an active script on an active gameobject) is only important if you need to catch it. A case of “if a tree falls in a forest and there is no-one there to hear it, does it make any sound?” :slight_smile:

To use it you’d have something like:

	void OnApplicationPause(bool paused)
	{
		Debug.Log("APP PAUSED: "+ paused);  // prints APP PAUSED: True when paused
		if(!paused)
		{
			Debug.Log("RESUMING...");
		}
	}

Place this on any active script and it will be called on each one when you click away from Unity and back in Play mode.

Just to be clear, it’s not a method or function you’d call, you simply listen for it. It’s invoked automatically by the application when it loses focus and the application is forced to suspend itself, and when it regains focus. On iOS for example this would happen when you press the home button - not sure how it works with desktop and webplayer builds, but I’d assume it’s called if the application doesn’t have “Run In Background” set to true and it loses focus.

Hi, I know I am like 12 years late but for anyone like me that was just looking for the answer here it is:

private void OnApplicationPause(bool pauseStatus)
    {
        if (pauseStatus == true)
        {
           // Put your code here
        }
    }
4 Likes

You save my day, bro

Please try to use the like button to show your appreciation. This means the thread won’t be necroed for everyone.

Thanks.