Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to correctly convert Prefab to Entity from MonoBehaviour? ConvertGameObjectHierarchy issues.

Discussion in 'Entity Component System' started by Armitage1982, Sep 28, 2022.

  1. Armitage1982

    Armitage1982

    Joined:
    Jul 26, 2012
    Posts:
    37
    Hi,

    Why when converting a GameObject Prefab from MonoBehaviour like this :
    Code (CSharp):
    1.         public GameObject MyPrefab;
    2.         private Entity prefab;
    3.         void Start()
    4.         {
    5.             var world = World.DefaultGameObjectInjectionWorld;
    6.             ConvertToEntitySystem convertToEntitySystem = world.GetExistingSystem<ConvertToEntitySystem>();
    7.             GameObjectConversionSettings settings = GameObjectConversionSettings.FromWorld(world, convertToEntitySystem.BlobAssetStore);
    8.             prefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(MyPrefab, settings);
    9.         }
    Does it throw :
    InvalidOperationException: The entity does not exist.

    in another unrelated Authoring class trying to add a simple TAG components with entityManager.AddComponents ?

    I used to instantiate with Object.Instantiate a GameObject that had a ConvertToEntity component and everything worked fine, so I guess this new code above is the problem (if I comment the GameObjectConversionUtility.ConvertGameObjectHierarchy line, the error no longer occurs).


    I will probably use the solution provided in this topic to get around that issue:
    https://forum.unity.com/threads/get...o-within-a-monobehaviour.809490/#post-5374581

    Is there anything in the 0.51.1-Preview.21 that needs to be changed to instantiate from a MonoBehaviour?

    The OP mentions that instantiating a GO with physic stuff is a problem and I do use Havok.

    Thanks
     
    MrKsi likes this.
  2. MrKsi

    MrKsi

    Joined:
    Aug 30, 2020
    Posts:
    134
    I have such a problem myself now, that is, when I converted the mesh into an entity, I received this error message, because Mesh Collider is somehow connected to the Entity and cannot be compatible, then I moved the creation of the Mesh after the creation of the Entity and everything worked, until today I don't understand the moment. Please let me know if you find a solution while I'm looking for it myself
     
  3. Armitage1982

    Armitage1982

    Joined:
    Jul 26, 2012
    Posts:
    37
    I haven't found a solution, especially since the new version of Entities 1.0.0 opts for a brand new conversion system (bake). So it seems less likely to me to find this solution now that 0.51.1 is deprecated.

    I opted for the workaround proposed in this topic.

    That is to say, an Authoring that retrieves the converted Entity in the field of an IComponentData and then, in the MonoBehaviour, I use the Instantiate method of the entity manager with a Query that retrieves the first converted element. It is thus necessary that at least one prefab, having the IComponentData, is present (instantiated) beforehand in the scene.
    The advantage here is that since there is a Query, I can refine it with its methods, like Where(), and do without a direct reference to the prefab.

    Code (CSharp):
    1.  
    2. public enum ItemType { Default, ItemType1, ItemType2, ItemType3 }
    3.  
    4. public struct ItemTypeData : IComponentData
    5. {
    6.     public ItemType itemType;
    7.     public Entity Entity;
    8. }
    9.  
    10. public class ItemTypeAuthoring : MonoBehaviour, IConvertGameObjectToEntity
    11. {
    12.     public ItemType itemType = ItemType.Default;
    13.  
    14.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    15.     {
    16.         // Add a remover level
    17.         dstManager.AddComponentData(entity, new ItemTypeData
    18.         {
    19.            itemType = itemType,
    20.            Entity = conversionSystem.GetPrimaryEntity(this)
    21.         });
    22.     }
    23. }
    24.  
    Then in MonoBehaviour
    Code (CSharp):
    1.  
    2. public void Spawn()
    3. {
    4.     var query = entityManager.CreateEntityQuery(typeof(ItemTypeData)).ToComponentDataArray<ItemTypeData>(Allocator.Temp);
    5.     var entity = query.Where((x) => x.itemType == ItemTypeToInstantiate).First().Entity;
    6.     var instance = entityManager.Instantiate(entity);
    7.  
    8.     // Transform the instantiated entity in world space
    9.     entityManager.SetComponentData(instance, new Translation { Value = transform.position });
    10.     entityManager.SetComponentData(instance, new Rotation { Value = transform.rotation });
    11. }
    12.  
    It works and it is indeed much faster than creating a Pool system.
     
    Last edited: Sep 30, 2022
    MrKsi likes this.
  4. MrKsi

    MrKsi

    Joined:
    Aug 30, 2020
    Posts:
    134
    It's also funny that I still have all the GameObject converted into an Entity with all the necessary components, but for some reason raycast still refuses to detect them when I shoot a glocket to make a hole)
     
  5. MrKsi

    MrKsi

    Joined:
    Aug 30, 2020
    Posts:
    134
    In general, I understood why I had an error when converting, it turns out that I had a project that recently crashed, was completely destroyed and I had to restore it (a year of work) I had a lot of scripts that were not assigned (Missing Script) I cleaned them because there was no need, and all at once it worked, maybe you also have a lot of yellow icons and this error may occur because of it, in any case, thank you for your find