Exception thrown during SubScene import: System.InvalidCastException: Specified cast is not valid.

Hello,

Recently I’ve been getting this error a lot when making changes to subscenes, which causes the subscene to completely fail to load:

It’s really annoying to get the subscene loading correctly again once this happens. Sometimes I can just make a random change and re-save and it fixes it. Other times I have had to revert with version control and restart Unity.

3 Likes

Hello,
Restart Unity project helps to fix it )

I also have this problem now with a different error. I don’t know where to start to fix:

Exception thrown during SubScene import: System.InvalidCastException: Specified cast is not valid.
  at (wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr)
  at System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer[TDelegate] (System.IntPtr ptr) [0x00010] in <6122523d36af4c6bbd1e418f9bebd8ba>:0 
  at Unity.Burst.FunctionPointer`1[T].get_Invoke () [0x00008] in .\Library\PackageCache\com.unity.burst\Runtime\FunctionPointer.cs:68 
  at Unity.Entities.WorldUnmanagedImpl..ctor (Unity.Entities.World worldManaged, System.UInt64 sequenceNumber, Unity.Entities.WorldFlags flags, Unity.Collections.AllocatorHelper`1[T] worldAllocatorHelper, System.String worldName) [0x000a2] in .\Library\PackageCache\com.unity.entities\Unity.Entities\WorldUnmanaged.cs:254 
  at Unity.Entities.WorldUnmanaged.Create (Unity.Entities.World world, Unity.Entities.WorldFlags flags, Unity.Collections.AllocatorManager+AllocatorHandle backingAllocatorHandle) [0x00069] in .\Library\PackageCache\com.unity.entities\Unity.Entities\WorldUnmanaged.cs:1017 
  at Unity.Entities.World.Init (Unity.Entities.WorldFlags flags, Unity.Collections.AllocatorManager+AllocatorHandle backingAllocatorHandle) [0x00012] in .\Library\PackageCache\com.unity.entities\Unity.Entities\World.cs:238 
  at Unity.Entities.World..ctor (System.String name, Unity.Entities.WorldFlags flags) [0x0003e] in .\Library\PackageCache\com.unity.entities\Unity.Entities\World.cs:213 
  at Unity.Scenes.Editor.EditorEntityScenes.BakeAndWriteEntityScene (UnityEngine.SceneManagement.Scene scene, Unity.Entities.BakingSettings settings, System.Collections.Generic.List`1[T] sectionRefObjs, Unity.Scenes.Editor.WriteEntitySceneSettings writeEntitySettings) [0x00001] in .\Library\PackageCache\com.unity.entities\Unity.Scenes.Editor\EditorEntityScenes.cs:61 
  at Unity.Scenes.Editor.SubSceneImporter.ImportBaking (UnityEditor.AssetImporters.AssetImportContext ctx, UnityEngine.SceneManagement.Scene scene, Unity.Scenes.SceneWithBuildConfigurationGUIDs sceneWithBuildConfiguration, Unity.Entities.Build.IEntitiesPlayerSettings settingsAsset, UnityEngine.GameObject prefab) [0x0007c] in .\Library\PackageCache\com.unity.entities\Unity.Scenes.Editor\SubSceneImporter.cs:123 
  at Unity.Scenes.Editor.SubSceneImporter.OnImportAsset (UnityEditor.AssetImporters.AssetImportContext ctx) [0x000d8] in .\Library\PackageCache\com.unity.entities\Unity.Scenes.Editor\SubSceneImporter.cs:202 
 -> (artifact id: '7ae1a39103d083888624f6e830654d73') in 2.6456286 seconds

Clearing the entities cache fixed the issue.