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

EnsureRunningOnMainThread can only be called from the main thread.

Discussion in '5.4 Beta' started by alexandre-fiset, Apr 8, 2016.

  1. alexandre-fiset

    alexandre-fiset

    Joined:
    Mar 19, 2012
    Posts:
    715
    Hi!

    When I build my game with DX12, it works but crashes once I try to launch the game.

    Now I tried to build it with AutoGraphic API, but I get this error message and the build can't complete:

    « EnsureRunningOnMainThread can only be called from the main thread.
    Constructors and field initializers will be executed from the loading thread when loading a scene.
    Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function. »

    There is no way to catch there this comes from, help? At this rate maybe I'll be able to play a build in a month :p
     
  2. lukaszunity

    lukaszunity

    Administrator

    Joined:
    Jun 11, 2014
    Posts:
    461
    Do you have a call stack for the EnsureRunningOnMainThread error?
     
  3. alexandre-fiset

    alexandre-fiset

    Joined:
    Mar 19, 2012
    Posts:
    715
    From what I remember I don't think so since I'd have shared it... But now that I try it again in b14, it obviously works. I do get a crash in-game, but it's another issue.
     
  4. ottolb

    ottolb

    Joined:
    Jul 1, 2009
    Posts:
    42
    Hi,
    I'm receiving this error when building for iOS, restarting Unity solve the problem.

    Code (JavaScript):
    1. EnsureRunningOnMainThread is not allowed to be called during serialization, call it from Awake or Start instead. Called from MonoBehaviour 'Garage' on game object '5 Garage'.
    2. See "Script Serialization" page in the Unity Manual for further details.
    3. UnityEngine.Object:op_Inequality(Object, Object)
    4. UnityEditorInternal.AnimationWindowSelectionItem:get_animationPlayer() (at /Users/builduser/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowSelectionItem.cs:45)
    5. UnityEditorInternal.StandaloneSelectionItem:get_animationClip() (at /Users/builduser/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/StandaloneSelectionItem.cs:24)
    6. UnityEditorInternal.<CurveWasModified>c__AnonStorey49:<>m__8C(AnimationWindowSelectionItem) (at /Users/builduser/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowState.cs:339)
    7. System.Collections.Generic.List`1:Exists(Predicate`1)
    8. UnityEditorInternal.AnimationWindowSelection:Exists(Predicate`1) (at /Users/builduser/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowSelection.cs:199)
    9. UnityEditorInternal.AnimationWindowState:CurveWasModified(AnimationClip, EditorCurveBinding, CurveModifiedType) (at /Users/builduser/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowState.cs:339)
    10. UnityEditor.HostView:OnGUI()
    11.  
     
  5. lukaszunity

    lukaszunity

    Administrator

    Joined:
    Jun 11, 2014
    Posts:
    461
    Hi, could you file a bug with a repro project and post the case number here?
     
    ottolb likes this.
  6. ottolb

    ottolb

    Joined:
    Jul 1, 2009
    Posts:
    42
    Here it's: Case 792341
    Let me know if you need something else.
     
  7. shawnblais

    shawnblais

    Joined:
    Oct 11, 2012
    Posts:
    324
    Getting this too, from a script that has [ExecuteInEdit] mode on it. Tried commenting that out, and commenting out the entire class, and it still throws this error and will not build :(

    Rebooted, and now it refuses to build saying the openvr_api.dll already exists... le sigh...
     
    Last edited: Apr 28, 2016
  8. duhprey

    duhprey

    Joined:
    Nov 13, 2009
    Posts:
    166
    I also had this problem. The work around for me is to close the Animation tab when I'm building.
     
    ottolb likes this.
  9. ottolb

    ottolb

    Joined:
    Jul 1, 2009
    Posts:
    42
    Thanks! For me it solved too.
     
  10. mattparkins

    mattparkins

    Joined:
    Apr 1, 2014
    Posts:
    31
    Ah yes, closing the Animation tab. Pretty straight forward and obvious when you think about it really. :eek:
     
  11. stevenc33

    stevenc33

    Joined:
    Apr 20, 2016
    Posts:
    18
    I'm getting this error too when loading a project with MapMagic 1.2 on Unity 5.4.0b17.

    Here is the call stack:

    UnityException: EnsureRunningOnMainThread is not allowed to be called during serialization, call it from Awake or Start instead. Called from MonoBehaviour 'MapMagic' on game object 'Map Magic'.
    See "Script Serialization" page in the Unity Manual for further details.
    UnityEngine.Object.GetInstanceID () (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:31)
    MapMagic.Serializer+SerializedObject.GetValue (System.Type type, System.String name, MapMagic.Serializer ser) (at Assets/MapMagic/Main/Serializer.cs:114)
    MapMagic.Serializer.Retrieve (Int32 num) (at Assets/MapMagic/Main/Serializer.cs:263)
    MapMagic.Serializer+SerializedObject.GetValue (System.Type type, System.String name, MapMagic.Serializer ser) (at Assets/MapMagic/Main/Serializer.cs:121)
    MapMagic.Serializer.Retrieve (Int32 num) (at Assets/MapMagic/Main/Serializer.cs:263)
    MapMagic.Serializer+SerializedObject.GetValue (System.Type type, System.String name, MapMagic.Serializer ser) (at Assets/MapMagic/Main/Serializer.cs:121)
    MapMagic.Serializer.Retrieve (Int32 num) (at Assets/MapMagic/Main/Serializer.cs:263)
    MapMagic.Serializer+SerializedObject.GetValues (System.Type elementType, MapMagic.Serializer ser) (at Assets/MapMagic/Main/Serializer.cs:140)
    MapMagic.Serializer.Retrieve (Int32 num) (at Assets/MapMagic/Main/Serializer.cs:245)
    MapMagic.Serializer+SerializedObject.GetValue (System.Type type, System.String name, MapMagic.Serializer ser) (at Assets/MapMagic/Main/Serializer.cs:121)
    MapMagic.Serializer.Retrieve (Int32 num) (at Assets/MapMagic/Main/Serializer.cs:263)
    MapMagic.MapMagic.OnAfterDeserialize () (at Assets/MapMagic/MapMagic.cs:779)
     
  12. BrainAndBrain

    BrainAndBrain

    Joined:
    Nov 27, 2014
    Posts:
    115
    Also received this error, running Unity 5.4.0b17. Stack:

    EnsureRunningOnMainThread is not allowed to be called during serialization, call it from Awake or Start instead. Called from MonoBehaviour 'FishingVillage' on game object 'room_FishingVillage'.
    See "Script Serialization" page in the Unity Manual for further details.
    UnityEngine.Object:eek:p_Inequality(Object, Object)
    UnityEditorInternal.AnimationWindowSelectionItem:get_animationPlayer() (at /Users/builduser/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowSelectionItem.cs:45)
    UnityEditorInternal.StandaloneSelectionItem:get_animationClip() (at /Users/builduser/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/StandaloneSelectionItem.cs:24)
    UnityEditorInternal.<CurveWasModified>c__AnonStorey4A:<>m__8D(AnimationWindowSelectionItem) (at /Users/builduser/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowState.cs:339)
    System.Collections.Generic.List`1:Exists(Predicate`1)
    UnityEditorInternal.AnimationWindowSelection:Exists(Predicate`1) (at /Users/builduser/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowSelection.cs:199)
    UnityEditorInternal.AnimationWindowState:CurveWasModified(AnimationClip, EditorCurveBinding, CurveModifiedType) (at /Users/builduser/buildslave/unity/build/Editor/Mono/Animation/AnimationWindow/AnimationWindowState.cs:339)
    UnityEditor.HostView:OnGUI()

    As was the experience of others, closing the Animation tab allowed me to build.
     
  13. stevenc33

    stevenc33

    Joined:
    Apr 20, 2016
    Posts:
    18
    Update. I moved the code inside MapMagic.MapMagic.OnAfterDeserialize () (at Assets/MapMagic/MapMagic.cs:779) to Awake(). And all has been well.
     
  14. buronix

    buronix

    Joined:
    Dec 11, 2013
    Posts:
    111
    Im getting the same error :

    UnityException: EnsureRunningOnMainThread can only be called from the main thread
    UnityEngine.Object.GetInstanceID () (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:31)
    UnityEngine.Object.IsNativeObjectAlive (UnityEngine.Object o) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:126)
    UnityEngine.Object.CompareBaseObjects (UnityEngine.Object lhs, UnityEngine.Object rhs) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:90)
    UnityEngine.Object.op_Inequality (UnityEngine.Object x, UnityEngine.Object y) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:193)
    BEngine.BTerrain.Biomes.Biome.hasSuplementaryTextureMap () (at Assets/Scripts/Engine/Terrain/Biomes/Biome.cs:343)
    BEngine.BTerrain.Chunks.TerrainChunk.CreateTerrainData () (at Assets/Scripts/Engine/Terrain/Chunks/TerrainChunk.cs:152)
    BEngine.BTerrain.Chunks.ChunkCache.CreateTerrainDataConsumer () (at Assets/Scripts/Engine/Terrain/Chunks/ChunkCache.cs:413)
    UnityEngine.Debug:LogException(Exception)
    BEngine.BTerrain.Chunks.ChunkCache:CreateTerrainDataConsumer() (at Assets/Scripts/Engine/Terrain/Chunks/ChunkCache.cs:420)

    the function is :

    Code (CSharp):
    1. public bool hasSuplementaryTextureMap()
    2.         {
    3.             if (SuplementarySplatTexture != null && SuplementaryTextureType != SuplementaryType.None)
    4.             {
    5.                 if (SuplementaryTextureType == SuplementaryType.BlendNoise || SuplementaryTextureType == SuplementaryType.SwitchNoise)
    6.                 {
    7.                     if (SuplementaryNoise != null)
    8.                     {
    9.                         return true;
    10.                     }
    11.                     else
    12.                     {
    13.                         return false;
    14.                     }
    15.                 }
    16.                 return true;
    17.             }
    18.             return false;
    19.         }
    Im just checking numm, and comparing enums.
    I cant understand why this, Im on Unity 5.4.0b21 (64-bit), just testing my 5.3.4 project on 5.4.

    Animator tab is closed.
     
  15. lukaszunity

    lukaszunity

    Administrator

    Joined:
    Jun 11, 2014
    Posts:
    461
    Are you running this on a separate thread? This is triggered by comparing (== and != operator) UnityEngine.Object derived objects on another thread besides the main thread, which is not safe.
     
  16. buronix

    buronix

    Joined:
    Dec 11, 2013
    Posts:
    111
    Yes Im running it on a separate thread, this is new of unity 5.4 this was not happening un 5.3, I found The problemas is when I check if a texture is null, I think its very problematic, I made a workaround, I can understand that check a Unity Object status outside The main thread can be problematic, but I think an error exception is too much, a warning would be enought, now I need to spent lot of time rewriting my conde without need (The texture is only declared at start its not changing un runtime) I think you are overreacting with this implementation.