Skip to main content
Commonmark migration
Source Link

My best guess based on the docs:

EditorApplication.playModeStateChanged

 

Add an event handler to this event to receive a notification that the play mode state has changed

Note the use of past tense - this event promises to notify you only after the state has changed. It's not a playModeStateWillChange event.

But clearly your static constructor is getting called earlier, thanks to the [InitializeOnLoad] attribute:

Note that static constructors with this attribute are called when Unity starts and when Run is pressed. In this second case the Unity runtime is intialised and this is treated as a Load.

So it looks like, rather than registering to handle the event after the editor finishes starting playmode (which apparently includes calling Start() for all MonoBehaviours in the open scenes), you could act immediately in the static constructor.

Since this constructor also fires when the editor loads, you might need to check:

EditorApplication.isPlayingOrWillChangePlaymode

 

Is editor either currently in play mode, or about to switch to it? (Read Only)

 

This will return true when editor will switch to play mode after finishing some tasks (e.g. after scripts will be recompiled).

to verify whether this initialization is due to the editor loading or a play mode switch.

Give that a shot, and let us know if that helps you get your needed actions in before Start() happens.

My best guess based on the docs:

EditorApplication.playModeStateChanged

 

Add an event handler to this event to receive a notification that the play mode state has changed

Note the use of past tense - this event promises to notify you only after the state has changed. It's not a playModeStateWillChange event.

But clearly your static constructor is getting called earlier, thanks to the [InitializeOnLoad] attribute:

Note that static constructors with this attribute are called when Unity starts and when Run is pressed. In this second case the Unity runtime is intialised and this is treated as a Load.

So it looks like, rather than registering to handle the event after the editor finishes starting playmode (which apparently includes calling Start() for all MonoBehaviours in the open scenes), you could act immediately in the static constructor.

Since this constructor also fires when the editor loads, you might need to check:

EditorApplication.isPlayingOrWillChangePlaymode

 

Is editor either currently in play mode, or about to switch to it? (Read Only)

 

This will return true when editor will switch to play mode after finishing some tasks (e.g. after scripts will be recompiled).

to verify whether this initialization is due to the editor loading or a play mode switch.

Give that a shot, and let us know if that helps you get your needed actions in before Start() happens.

My best guess based on the docs:

EditorApplication.playModeStateChanged

Add an event handler to this event to receive a notification that the play mode state has changed

Note the use of past tense - this event promises to notify you only after the state has changed. It's not a playModeStateWillChange event.

But clearly your static constructor is getting called earlier, thanks to the [InitializeOnLoad] attribute:

Note that static constructors with this attribute are called when Unity starts and when Run is pressed. In this second case the Unity runtime is intialised and this is treated as a Load.

So it looks like, rather than registering to handle the event after the editor finishes starting playmode (which apparently includes calling Start() for all MonoBehaviours in the open scenes), you could act immediately in the static constructor.

Since this constructor also fires when the editor loads, you might need to check:

EditorApplication.isPlayingOrWillChangePlaymode

Is editor either currently in play mode, or about to switch to it? (Read Only)

This will return true when editor will switch to play mode after finishing some tasks (e.g. after scripts will be recompiled).

to verify whether this initialization is due to the editor loading or a play mode switch.

Give that a shot, and let us know if that helps you get your needed actions in before Start() happens.

Source Link
DMGregory
  • 141k
  • 23
  • 258
  • 401

My best guess based on the docs:

EditorApplication.playModeStateChanged

Add an event handler to this event to receive a notification that the play mode state has changed

Note the use of past tense - this event promises to notify you only after the state has changed. It's not a playModeStateWillChange event.

But clearly your static constructor is getting called earlier, thanks to the [InitializeOnLoad] attribute:

Note that static constructors with this attribute are called when Unity starts and when Run is pressed. In this second case the Unity runtime is intialised and this is treated as a Load.

So it looks like, rather than registering to handle the event after the editor finishes starting playmode (which apparently includes calling Start() for all MonoBehaviours in the open scenes), you could act immediately in the static constructor.

Since this constructor also fires when the editor loads, you might need to check:

EditorApplication.isPlayingOrWillChangePlaymode

Is editor either currently in play mode, or about to switch to it? (Read Only)

This will return true when editor will switch to play mode after finishing some tasks (e.g. after scripts will be recompiled).

to verify whether this initialization is due to the editor loading or a play mode switch.

Give that a shot, and let us know if that helps you get your needed actions in before Start() happens.