Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by voldemarz · Jul 13, 2015 at 05:25 PM · editorscenepostprocessorder-of-execution

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
 }
 

alt text

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.

example.png (50.7 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zach-r-d · Jul 13, 2015 at 09:40 PM 0
Share

Good question, +1. I'm going to investigate this.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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().

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image voldemarz · Jul 13, 2015 at 11:17 PM 0
Share

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.

avatar image zach-r-d · Jul 14, 2015 at 02:04 AM 0
Share

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.

avatar image voldemarz · Jul 14, 2015 at 07:31 AM 0
Share

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.

avatar image voldemarz · Jul 14, 2015 at 07:45 AM 0
Share

$$anonymous$$ultiple post process calls is a registered bug http://issuetracker.unity3d.com/issues/postprocessscene-called-twice-during-a-build

avatar image zach-r-d · Jul 14, 2015 at 10:49 AM -1
Share

Alright, sure. I'm out.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges