When baking Adaptive Probe Volumes I consistently hit an exception coming from Unity’s ray tracing acceleration structure build step. The bake fails with:
InvalidCastException: Specified cast is not valid.
UnityEngine.Rendering.UnifiedRayTracing.AccelStructAdapter.AddInstance
(System.Int32 objectHandle, UnityEngine.Component meshRendererOrTerrain, ...)
(at ./Library/PackageCache/com.unity.rendering.light-...
Workaround / Temporary Fix
Disabling Virtual Offset (on the APV bake settings) avoids the error and allows the bake to complete.
Root cause (what object triggered it)
To identify which object was being passed into AddInstance, I added a log inside Unity’s package code:
File:
Library/PackageCache/.../AccelStructAdapter.cs
Added:
Debug.Log(meshRendererOrTerrain);
This revealed that the component being passed in wasn’t always a MeshRenderer/Terrain as the parameter name suggests. In my case, Unity was attempting to add a Particle System related component from a prefab where the VFX lived on the same GameObject as the mesh renderer.
So instead of the mesh renderer on the prefab root being used, the acceleration structure path sometimes picked up the particle effect component on that same GameObject, which appears to lead to an invalid cast. One additional detail I’m still investigating: the prefab also has a MeshCollider component that is disabled but still present . I have not confirmed yet whether the presence of a disabled MeshCollider contributes to (or triggers) the wrong component being selected. This is currently TBD.
Fix I used (project-side fix)
I moved the particle effect off the prefab root and onto a child GameObject.
Before:
-
Prefab root GameObject contained:
- MeshRenderer / SkinnedMeshRenderer (the actual visible mesh)
- ParticleSystem (or particle-related renderer/component)
After:
-
Prefab root GameObject contains only the mesh renderer(s)
-
ParticleSystem moved to a child:
-
Root (MeshRenderer)VFX (ParticleSystem)
-
After this change:
- Adaptive Probe Volume bake works with Virtual Offset enabled
- The
InvalidCastExceptionno longer occurs