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. Unity Issue Tracker - 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.