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
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
Comments (6)
-
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 -
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.
-
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?
-
Long2904
Feb 01, 2021 14:35
Still having this in 2019.4.16f1
-
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?
-
Vanilla-Plus
Nov 01, 2020 08:30
Still happening in 2020.1.8f1
Add comment
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- DownloadHandlerScript.CompleteContent is called twice when building for WebGL
- Scene view has Y coordinates of the Screen Position node flipped when some of the URP features are disabled
- Volumetric fog shader variants are missing from build when "Strict Shader Variant Matching" is disabled
- Unnecessary modifications clutter the Scene when using a RectTransform driven by a LayoutGroup in a Prefab
- Files in the target folder are deleted without a proper warning when building an iOS project
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;
}
}
}