Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Bug OnEnable Is Called Twice For MonoBehaviours With The [ExecuteAlways] Attribute

Discussion in '2021.2 Beta' started by Nexer8, Sep 11, 2021.

  1. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    When entering playmode, OnEnable will be called twice for MonoBehaviours with the [ExecuteAlways] attribute. Aside from performance problems of double initialization, the first OnEnable also does not follow the execution order of the script.

    In my case, I have a UI Toolkit UIDocument, where I want to add VisualElements to the rootVisualElement.
    In EditMode, this works just fine because OnEnable is called after the UIDocument.
    In PlayMode, this throws errors because OnEnable is called twice. Once some time before the UI Document and once when it should be called.

    Expected behavior is that a script with execution order of -50 should execute after a script with execution order of -100.

    Code (CSharp):
    1. [DefaultExecutionOrder(-50)]
    2. public class ExampleCanvas : MonoBehaviour
    3. {
    4.     //Assigned in the inspector
    5.     public UIDocument document;//Execution order of -100
    6.  
    7.     private void OnEnable()
    8.     {
    9.         Debug.Log("Enable");//This will log "Enable" twice
    10.  
    11.         document.rootVisualElement.Add(new VisualElement());
    12.  
    13.         //First OnEnable:
    14.         //[Error] UIDocument has yet to be initialized, so rootVisualElement is still null
    15.  
    16.         //Second OnEnable:
    17.         //[No Error] UIDocument has been initialized, so rootVisualElement is no longer null
    18.     }
    19. }
     
  2. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,136
  3. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    How does that work with Assets? In my case, I need the scripts to work in a certain order and if that order is not preserved for the users, or if the users can change it, then that would be a bad thing.
     
  4. mahdi_jeddi

    mahdi_jeddi

    Joined:
    Jul 18, 2016
    Posts:
    246
    The execution order of a script is saved in its .meta file. So it's preserved properly.
     
    Nexer8 likes this.
  5. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    Good. Do you know if derived classes also have this execution order or if I have to manually set it for all of them?
     
  6. mahdi_jeddi

    mahdi_jeddi

    Joined:
    Jul 18, 2016
    Posts:
    246