GameObjectConversionUtility.ConvertGameObjectHierarchy on same changed prefab disposes collider

Using GameObjectConversionUtility.ConvertGameObjectHierarchy on the same prefab twice, which in-between you change CollisionFilter, causes the PysicsComponent blobasset to become invalid on entity prefabs converted from the same gameObject prefab.

Hi! Can you please provide a very simple script demonstrating the issue? We’d need to see the use case to reason about what the behavior should be.

That said, if this is blocking you, I would suggest you instead create a separate prefab variant for the alternate set of collision filters. (I’d also advise avoiding using GameObjectConversionUtility.ConvertGameObjectHierarchy() at run-time and instead do something like the object spawner sample script: https://github.com/Unity-Technologies/EntityComponentSystemSamples/blob/master/UnityPhysicsSamples/Assets/Common/Scripts/SpawnRandomObjectsAuthoring.cs)

Some simple code. My use case is that I parent dynamic physics objects to simulate interior of moving entities, using layers, causing it to be in “parent space”. (spaceships with interior) So all things that can be inside the spaceships will need these different layers as there are multiple ships. So sure I could work around by creating X amount of layers prefabs manually or by some editor script, but it’s not a nice solution to do for 20-30 different interior things.

using System.Collections;
using System.Collections.Generic;
using Unity.Entities;
using Unity.Physics.Authoring;
using UnityEngine;

public class FilterSpawningDataAuthoring : MonoBehaviour, IConvertGameObjectToEntity
{
    public GameObject prefab;

    public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    {
        var filter = prefab.GetComponent<PhysicsShapeAuthoring>();

        List<Entity> entities = new List<Entity>();

        // 10 for different layers
        for (int i = 0; i < 10; i++)
        {
            filter.BelongsTo = new PhysicsCategoryTags()
            {
                Value = (uint)(1 << i)
            };
            filter.CollidesWith = new PhysicsCategoryTags()
            {
                Value = (uint)(1 << i)
            };
            entities.Add(GameObjectConversionUtility.ConvertGameObjectHierarchy(prefab, conversionSystem.ForkSettings((byte)(i + 1))));
        }

        // Avoid dealloc on buffer
        var buffer = dstManager.AddBuffer<FilterSpawningDataElement>(entity);
        for (int i = 0; i < entities.Count; i++)
        {
            buffer.Add(new FilterSpawningDataElement() { filterPrefab = entities[i] });
        }
    }
}

public struct FilterSpawningDataElement : IBufferElementData
{
    public Entity filterPrefab;
}

public class SpawnSystem : ComponentSystem
{
    EntityQuery m_group;
    protected override void OnCreate()
    {
        base.OnCreate();
        m_group = GetEntityQuery(ComponentType.ReadOnly<FilterSpawningDataElement>());
    }
    protected override void OnUpdate()
    {
        Entities.With(m_group).ForEach((Entity entity) =>
        {
            var buffer = EntityManager.GetBuffer<FilterSpawningDataElement>(entity);

            for (int i = 0; i < buffer.Length; i++)
            {
                PostUpdateCommands.Instantiate(buffer[i].filterPrefab);
            }
            PostUpdateCommands.RemoveComponent<FilterSpawningDataElement>(entity);
        });
    }
}

Also simple project
https://drive.google.com/drive/u/0/folders/1bQKXYRL1UwMNtcNB6qe3YZ8C3jOQlwkG