- Home /
Why is PostProcessScene called after Awake in editor?
Why is OnPostprocessScene called after Awake for scene objects when starting a play session in editor? Shouldn't it be before Awake? This behaviour is present in Unity 4.6.6p3 and 5.1.1f1.
I wanted to improve this nested prefab script to handle also objects that are not in scene, e.g. instantiate hierarchy of prefabs in Awake. The prefab is instantiated in OnPostprocessScene and attached to a parent. I wanted to set a reference to the instantiated child to indicate that prefab doesn't need to be instantiated again once Awake is called for the first time.
Here's a simple test code and log
public class PrefabInstance : MonoBehaviour
{
[SerializeField] private GameObject _prefab;
[HideInInspector]
[SerializeField] private GameObject _prefabInstance;
void Awake()
{
Debug.Log("PrefabInstance.Awake() instance null? " + (_prefabInstance == null));
}
void OnEnable ()
{
Debug.Log("PrefabInstance.OnEnable " + name);
}
#if UNITY_EDITOR
[PostProcessScene(-2)]
public static void OnPostprocessScene()
{
Debug.Log("OnPostprocessScene");
foreach (PrefabInstance pi in UnityEngine.Object.FindObjectsOfType (typeof (PrefabInstance)))
{
GameObject go = GameObject.Instantiate(pi._prefab) as GameObject;
pi._prefabInstance = go;
go.transform.parent = pi.transform;
}
}
#endif
}
EDIT:
More on PostProcessScene behaviour:
When building/exporting as standalone, OnPostprocessScene is called twice, no Awake. Both post process apparently act on two different scenes since adding "Debug.Log("OnPostprocessScene() - " + pi.transform.childCount);" in the foreach loop above, report child count of 1 twice. UPDATE: This is a confirmed bug. http://issuetracker.unity3d.com/issues/postprocessscene-called-twice-during-a-build
When launching the standalone there's only Awake and OnEnable calls as expected. Verified by logging that the Loader object has only one child.
Answer by zach-r-d · Jul 13, 2015 at 10:03 PM
First, it's important to note that Awake() and OnEnable() are called immediately, in that order, whenever any object is instantiated. With that in mind, understanding this behavior is just a matter of noting that when building a scene, Unity first instantiates all of the objects in it, and then runs any post processing callbacks. If it didn't instantiate the objects first, there would be nothing for the post processing callbacks to operate on since scenes are built in a vacuum so as not to affect the original scene file/asset. It's also worth noting that in playmode, scenes are apparently built on the fly, so every time Application.loadLevel or one of its derivatives is called, OnPostprocessScene will be run again.
Anyway, all this is to say that any code that needs to be run after OnPostprocessScene needs to go in Start().
Thanks for response, but it doesn't really explain it.
Awake doesn't get called when building standalone executable
OnPostprocessScene is not called at runtime in a standalone build.
Other weird thing is that OnPostprocessScene gets called twice when building.
I've added these findings to the question.
I don't think Awake is needed for building the scene, scene objects should be created with default constructors and fields deserialized. Also, Awake is not called in edit mode unless script is marked with ExecuteInEdit mode attribute.
Yep, turns out the Awake and OnEnable I was seeing during building were from me adding ExecuteInEdit to the class while testing, and seeing them when the scene reloaded after the build finished; my bad. Removing that, it's clear that indeed, when making a build proper, OnPostprocessScene is called on just the objects with deserialized fields.
OnPostprocessScene is an editor-only callback, hence the #if, so indeed it will not be called at runtime.
Some experimentation has shown that OnPostprocessScene is called once for each scene included in the build, and then one extra time for scene 0. I do not know why scene 0 is built a second time.
In any case, given the behavior during building, I suppose the fact that OnPostprocessScene is called after Awake() and OnEnable() rather than before when in play mode is a bug.
Thanks for input. Let's hope someone who has used the given callback will confirm that. Documentation for it is very limited, so maybe I'm just misunderstanding something. Btw, Unity 4.6.6p3 behaves same way.
$$anonymous$$ultiple post process calls is a registered bug http://issuetracker.unity3d.com/issues/postprocessscene-called-twice-during-a-build
Your answer

Follow this Question
Related Questions
Drawing Handles 1 Answer
cannot open a unity scene 0 Answers
'Resource file has already been unloaded' error when exiting application in Editor 1 Answer
Editor scene preview window 0 Answers
Why does object movement get saved to scene editor? 2 Answers