Search Issue Tracker

Won't Fix

Votes

8

Found in

2017.4

2019.3.14f1

2020.2

Issue ID

1249609

Regression

No

Application.isPlaying is false when OnEnable is called from a ScriptableObject after entering the Play Mode

Scripting

-

How to reproduce:
1. Open the attached project
2. In the Project Window, select the "New Scriptable Object Sample"
3. Enter the Play Mode

Expected result: Application.isPlaying is true after OnEnable function is called
Actual result: Application.isPlaying is false after OnEnable function is called

Reproducible with: 2017.4.40f1, 2018.4.23f1, 2019.3.14f1, 2020.1.0b9, 2020.2.0a12

  1. Resolution Note (2021.2.X):

    Unfortunately fixing this behaviour will mean introducing a potentially breaking change.

    We do suggest using a custom method that checks for unity exit/enter playmode events https://docs.unity3d.com/2021.1/Documentation/ScriptReference/EditorApplication-playModeStateChanged.html and have - in this case - set to true. Something like this works and should work for your case:

    [CreateAssetMenu]
    [InitializeOnLoad]
    public class ScriptableObjectSample : ScriptableObject
    {
    public bool X { get; set; }

    void OnEnable()
    {
    EditorApplication.playModeStateChanged += LogPlayModeState;
    Debug.Log("Application.isPlaying " + Application.isPlaying + " - CustomApplicationIsPlaying " + X);

    if (X)
    {
    Debug.Log("This message never displays");
    }
    }

    private void OnDisable()
    {
    EditorApplication.playModeStateChanged -= LogPlayModeState;
    }

    void LogPlayModeState(PlayModeStateChange state)
    {
    Debug.Log("LogPlayModeState " + X);

    if (state == PlayModeStateChange.ExitingEditMode)
    {
    X = true;
    }
    else if (state == PlayModeStateChange.ExitingPlayMode)
    {
    X = false;
    }
    }
    }

Comments (6)

  1. DSivtsov

    Sep 01, 2022 21:39

    It's not a bug, this described in doc https://docs.unity3d.com/ScriptReference/Application-isPlaying.html
    "Note: In the Editor for ScriptableObject assets, this property will return false in OnEnable. After reloading the domain, when reloading assemblies, Unity invokes OnEnable on all ScriptableObject instances. This happens before isPlaying is set to true."
    This workaround is demanding only for Editor

  2. Adrian

    Jan 28, 2022 20:59

    I think it's easier to use EditorApplication.isPlayingOrWillChangePlaymode instead and wrap it (and the UnityEditor using statement) in UNITY_EDITOR conditional compilation blocks.

  3. Long2904

    Jul 31, 2021 08:18

    The Unity suggested method wouldn't work when you build the project. EditorApplication can't be in the build version. Does anyone if this "bug" still exist when building the game?

  4. Long2904

    Feb 01, 2021 14:35

    Still having this in 2019.4.16f1

  5. paulbettner

    Jan 05, 2021 06:53

    I just hit this bug too (and it was AWFUL to track down.) Can we please know whether this is expected behavior or a real bug?

  6. Vanilla-Plus

    Nov 01, 2020 08:30

    Still happening in 2020.1.8f1

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.