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
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  3. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug 1299399 - "Failed to build EntityScene" bug

Discussion in 'Project Tiny' started by Sarkahn, Dec 9, 2020.

  1. Sarkahn


    Jan 9, 2013
    Edit: Not sure if my dead script was even the original cause. Submitted a bug report. Details here.

    Hello, I just ran into this error. Here's a screenshot:


    After a lot of confusion I eventually figured out what was causing it - I had deleted one of my authoring components that was currently on some of my prefabs that were referenced in the scene. So first off - it would be really great if we could get a more helpful error message for this. Clicking build and seeing the error message "Failed to build entity scene" as my only hint basically makes me want to uninstall Unity forever.

    Anyways, aside from that I also ran into an issue with trying to get my builds to recognize that I had fixed the problem. I still have a pretty loose understanding about how conversion data gets cached so I'm only going to explain what occured and how I fixed it, whether it's a bug or this is how it's supposed to work, I have no idea. I want to be very clear that I made NO ACTUAL CHANGES to my project between these steps aside from the mentioned attribute changes.

    Step1 : Replace the dead scripts with new authoring scripts and save the prefabs.
    Step2 : Attempt dotnet build - same error.
    Step3 : Add a "ConverterVersion" attribute to my new authoring script that I just added, with a version of "1".
    Step4 : Attempt dotnet build - same error.
    Step5 : Increment the converter version to "2".
    Step6 : Attempt dotnet build - success!
    Step7 : Attempt wasm build - same error <- This doesn't seem right to me? Surely incrementing the version should have fixed it for both builds?
    Step8 : Increment the converter version AGAIN (even though I didn't actually make any changes) to "3".
    Step9 : Attempt wasm build - success!

    So yeah. Hopefully we can get a better error message for this case, or at least this might save someone from some pointless debugging.
    Last edited: Dec 15, 2020
    deus0 likes this.
  2. Sarkahn


    Jan 9, 2013
    Nevermind. After working on my project for a while I tried to build again and I'm getting the same error. I'd love to know how I can fix this.
  3. gwenaelle_unity


    Unity Technologies

    Jul 24, 2018
    I agree the error message is not very helpful here. We will fix that in the next version.
    Can you copy/paste your conversion system here, where you are adding the ConverterVersion attribute?
    Do you see anything suspicious in the AssetImportWorker.log file (ProjectDirectory/Logs)?
    Conversion data is cached in the asset database and is ran in a scripted importer when making a build.
    it gets retriggered if you are:
    modifying one of the scenes of the SceneList component.
    modifying a dependent asset (texture, font, mesh, ..) used in one of the scenes.
    modifying the build configuration used to build.
    incrementing of the version number of one of the conversion systems (see attribute ConverterVersion).
    Thanks for your feedback
    Last edited: Dec 10, 2020
  4. Sarkahn


    Jan 9, 2013
    Just hit this again. The previous times it happened I was able to get it to go away by incrementing the converter version on one of my authoring scripts, now that is not working.

    All of my conversion happens in
    scripts. They're all pretty basic, most do nothing more than add a component. This is the one I was referring to from the original post:

    Code (CSharp):
    1. using TinyRogue;
    2. using Unity.Entities;
    3. using UnityEngine;
    5. [ConverterVersion("PathBlockerAuthoring", 5)]
    6. public class PathBlockerAuthoring : MonoBehaviour, IConvertGameObjectToEntity
    7. {
    8.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    9.     {
    10.         dstManager.AddComponent<PathBlocker>(entity);
    11.     }
    12. }
    It looks like it's showing a null reference exception, I'm not sure what to parse from this though:

    Code (CSharp):
    1. Launched and connected shader compiler UnityShaderCompiler.exe after 0.03 seconds
    2. System.NullReferenceException: Object reference not set to an instance of an object
    3. This Exception was thrown from a job compiled with Burst, which has limited exception support. Turn off burst (Jobs -> Burst -> Enable Compilation) to inspect full exceptions & stacktraces
    4. (Filename:  Line: -1)
    6. Exception thrown during SubScene import: System.NullReferenceException: Object reference not set to an instance of an object
    7.   at Unity.Entities.EntityRemapUtility.RemapEntity (Unity.Entities.EntityRemapUtility+EntityRemapInfo* remapping, Unity.Entities.Entity source) [0x00000] in C:\Users\sark\Documents\unity\tinyrogue\Library\PackageCache\com.unity.entities@0.16.0-preview.21\Unity.Entities\EntityRemapUtility.cs:45
    8.   at Unity.Entities.EntityRemapUtility.RemapEntity (Unity.Collections.NativeArray`1[Unity.Entities.EntityRemapUtility+EntityRemapInfo]& remapping, Unity.Entities.Entity source) [0x00000] in C:\Users\sark\Documents\unity\tinyrogue\Library\PackageCache\com.unity.entities@0.16.0-preview.21\Unity.Entities\EntityRemapUtility.cs:40
    9.   at Unity.Entities.EntityRemapUtility.PatchEntities (Unity.Entities.EntityRemapUtility+EntityPatchInfo* scalarPatches, System.Int32 scalarPatchCount, Unity.Entities.EntityRemapUtility+BufferEntityPatchInfo* bufferPatches, System.Int32 bufferPatchCount, System.Byte* chunkBuffer, System.Int32 entityCount, Unity.Collections.NativeArray`1[Unity.Entities.EntityRemapUtility+EntityRemapInfo]& remapping) [0x000ac] in C:\Users\sark\Documents\unity\tinyrogue\Library\PackageCache\com.unity.entities@0.16.0-preview.21\Unity.Entities\EntityRemapUtility.cs:244
    10.   at Unity.Entities.Serialization.SerializeUtility.SerializeWorldInternal (Unity.Entities.EntityManager entityManager, Unity.Entities.Serialization.BinaryWriter writer, System.Object[]& referencedObjects, Unity.Collections.NativeArray`1[T] entityRemapInfos, System.Boolean isDOTSRuntime) [0x003d5] in C:\Users\sark\Documents\unity\tinyrogue\Library\PackageCache\com.unity.entities@0.16.0-preview.21\Unity.Entities\Serialization\SerializeUtility.cs:566
    11.   at Unity.Scenes.Editor.EditorEntityScenes.WriteEntityBinary (Unity.Entities.EntityManager scene, Unity.Scenes.ReferencedUnityObjects& objRefs, Unity.Collections.NativeArray`1[T] entityRemapInfos, System.String entitiesBinaryPath, Unity.Scenes.Editor.WriteEntitySceneSettings writeEntitySceneSettings) [0x00068] in C:\Users\sark\Documents\unity\tinyrogue\Library\PackageCache\com.unity.entities@0.16.0-preview.21\Unity.Scenes.Editor\EditorEntityScenes.cs:655
    12.   at Unity.Scenes.Editor.EditorEntityScenes.WriteEntitySceneSection (Unity.Entities.EntityManager scene, Unity.Entities.Hash128 sceneGUID, System.String subsection, UnityEditor.Experimental.AssetImporters.AssetImportContext importContext, Unity.Scenes.Editor.WriteEntitySceneSettings writeEntitySceneSettings, System.Int32& objectReferenceCount, Unity.Scenes.ReferencedUnityObjects& objRefs, Unity.Collections.NativeArray`1[T] entityRemapInfos) [0x0005a] in C:\Users\sark\Documents\unity\tinyrogue\Library\PackageCache\com.unity.entities@0.16.0-preview.21\Unity.Scenes.Editor\EditorEntityScenes.cs:580
    13.   at Unity.Scenes.Editor.EditorEntityScenes.WriteEntitySceneInternal (Unity.Entities.EntityManager entityManager, Unity.Entities.Hash128 sceneGUID, System.String sceneName, UnityEditor.Experimental.AssetImporters.AssetImportContext importContext, System.Int32 framesToRetainBlobAssets, System.Collections.Generic.List`1[T] sectionRefObjs, Unity.Scenes.Editor.WriteEntitySceneSettings writeEntitySceneSettings, Unity.Entities.Conversion.ConversionJournalData& journalData) [0x00359] in C:\Users\sark\Documents\unity\tinyrogue\Library\PackageCache\com.unity.entities@0.16.0-preview.21\Unity.Scenes.Editor\EditorEntityScenes.cs:361
    14.   at Unity.Scenes.Editor.EditorEntityScenes+<>c__DisplayClass11_0.<ConvertAndWriteEntitySceneInternal>b__0 (Unity.Entities.World conversionWorld) [0x00046] in C:\Users\sark\Documents\unity\tinyrogue\Library\PackageCache\com.unity.entities@0.16.0-preview.21\Unity.Scenes.Editor\EditorEntityScenes.cs:189
    15.   at Unity.Entities.GameObjectConversionUtility.ConvertScene (UnityEngine.SceneManagement.Scene scene, Unity.Entities.GameObjectConversionSettings settings) [0x00048] in C:\Users\sark\Documents\unity\tinyrogue\Library\PackageCache\com.unity.entities@0.16.0-preview.21\Unity.Entities.Hybrid\GameObjectConversion\GameObjectConversionUtility.cs:258
    16.   at Unity.Scenes.Editor.EditorEntityScenes.ConvertAndWriteEntitySceneInternal (UnityEngine.SceneManagement.Scene scene, Unity.Entities.GameObjectConversionSettings settings, System.Collections.Generic.List`1[T] sectionRefObjs, Unity.Scenes.Editor.WriteEntitySceneSettings writeEntitySettings) [0x00092] in C:\Users\sark\Documents\unity\tinyrogue\Library\PackageCache\com.unity.entities@0.16.0-preview.21\Unity.Scenes.Editor\EditorEntityScenes.cs:199
    17.   at Unity.Scenes.Editor.SubSceneImporter.OnImportAsset (UnityEditor.Experimental.AssetImporters.AssetImportContext ctx) [0x001c7] in C:\Users\sark\Documents\unity\tinyrogue\Library\PackageCache\com.unity.entities@0.16.0-preview.21\Unity.Scenes.Editor\SubSceneImporter.cs:154
    18. UnityEngine.DebugLogHandler:Internal_Log(LogType, LogOption, String, Object)
    19. UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
    20. UnityEngine.Logger:Log(LogType, Object)
    21. UnityEngine.Debug:Log(Object)
    22. Unity.Debug:Log(Object) (at Library\PackageCache\com.unity.entities@0.16.0-preview.21\Unity.Entities\Stubs\Unity\Debug.cs:17)
    23. Unity.Scenes.Editor.SubSceneImporter:OnImportAsset(AssetImportContext) (at Library\PackageCache\com.unity.entities@0.16.0-preview.21\Unity.Scenes.Editor\SubSceneImporter.cs:167)
    24. UnityEditor.Experimental.AssetImporters.ScriptedImporter:GenerateAssetData(AssetImportContext)
    25. (Filename: Library/PackageCache/com.unity.entities@0.16.0-preview.21/Unity.Entities/Stubs/Unity/Debug.cs Line: 17)
    27. -> (target hash: '74eb1d22cb084ac89b1966c50b878704') in 4.263658 seconds
    28.   Import took 4.275836 seconds .
  5. Sarkahn


    Jan 9, 2013
    This time I got it to go away by enabling a disabled gameobject in my scene, doing a build, then re-disabling it. And to re-iterate, in this case incrementing the ConverterVersion of an authoring script that was in my scene did nothing.
  6. Sarkahn


    Jan 9, 2013
    This keeps popping up. No matter how much I try to narrow it down, it really just seems completely random. At this point I'm not even sure if my dead script issue was the actual original cause.

    Not sure what I'm doing wrong but if I see this error one more time I'm going to lose my mind, so I created a bug report and I'll leave the project be for now. I tried to create a smaller repro case but I couldn't recreate it, so I just submitted the whole project. Case # 1299399.
  7. gwenaelle_unity


    Unity Technologies

    Jul 24, 2018
    I have looked again at your project, and I think I have found the issue.
    The problem is happening during scene serialization when we are trying to remap entities referenced in some components (ex: MapEntitiesBuffer has a entity reference). And the data here is wrong.
    Looking at the MapStateAuthoring conversion system that adds the MapEntitiesBuffer buffer to an entity, you are initializing it (size 400) and then do a Memclear on it that will reset the buffer. So the entities referenced in it actually don't exist, and we are failing to remap them.
    I would suggest instead to initialize your buffer at runtime.

    More generally make sure that the entity you are referencing in a component during conversion does exist in the destination world (can be Entity.Null (blank entity)) Or doesn't come from a different world, for example the gameobject world containing gameobject entities with component types coming from the gameobject components.

    In the next release, Any logs (+errors, exceptions) happening on the AssetImportWorker process will be forwarded directly to the Unity console. It will be easier to see errors happening than looking at a separate file (like a AssetImportWorker log file)
    tonialatalo, djsell and Sarkahn like this.
  8. Sarkahn


    Jan 9, 2013
    So I was under the assumption that doing a MemClear on a buffer of entities would be the equivalent of setting all the elements of the buffer to Entity.Null. Or in other words, doing a memclear on a buffer of structs would be the equivalent to setting every element of a buffer to "default". I'm far from an expert on "unsafe" stuff so I'm honestly not sure.

    Continuing with that assumption though, after looking at it I realized there was a bug in my MemClear code - I was failing to account for the size of the elements in the buffer. I wrote a test that seems to align with my assumptions:

    Code (CSharp):
    1.     [Test]
    2.     public void MemClearSuccess()
    3.     {
    4.         var em = EntityManager;
    6.         var e = em.CreateEntity();
    7.         var buff = em.AddBuffer<SomeBuffer>(e);
    9.         buff.ResizeUninitialized(400);
    10.         unsafe
    11.         {
    12.             UnsafeUtility.MemClear(buff.GetUnsafePtr(),
    13.                 buff.Length * UnsafeUtility.SizeOf<SomeBuffer>());
    14.         }
    16.         for(int i = 0; i < buff.Length; ++i)
    17.         {
    18.             Assert.AreEqual(Entity.Null, buff[i].entity);
    19.         }
    20.     }
    22.     [Test]
    23.     public void MemClearFail()
    24.     {
    25.         var em = EntityManager;
    27.         var e = em.CreateEntity();
    28.         var buff = em.AddBuffer<SomeBuffer>(e);
    30.         buff.ResizeUninitialized(400);
    31.         unsafe
    32.         {
    33.             UnsafeUtility.MemClear(buff.GetUnsafePtr(),
    34.                 buff.Length); // Not accounting for size of buffer elements
    35.         }
    37.         for (int i = 0; i < buff.Length; ++i)
    38.         {
    39.             Assert.AreEqual(Entity.Null, buff[i].entity);
    40.         }
    41.     }
    The first test passes, but the second test fails since (I assume, again not an expert on this stuff) that it's only clearing part of the buffer and leaving garbage memory in the rest.

    Either way, fixing up the memclear code seems to have fixed the bug though! And if it fails again I can just revert to initializing the buffers at runtime like you suggest. Thanks a lot for looking it this, I'm excited to be able to dive back into Tiny!
    tonialatalo likes this.