Why when converting a GameObject Prefab from MonoBehaviour like this :
public GameObject MyPrefab;
private Entity prefab;
void Start()
{
var world = World.DefaultGameObjectInjectionWorld;
ConvertToEntitySystem convertToEntitySystem = world.GetExistingSystem<ConvertToEntitySystem>();
GameObjectConversionSettings settings = GameObjectConversionSettings.FromWorld(world, convertToEntitySystem.BlobAssetStore);
prefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(MyPrefab, settings);
}
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:
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.
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
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.
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.
public enum ItemType { Default, ItemType1, ItemType2, ItemType3 }
public struct ItemTypeData : IComponentData
{
public ItemType itemType;
public Entity Entity;
}
public class ItemTypeAuthoring : MonoBehaviour, IConvertGameObjectToEntity
{
public ItemType itemType = ItemType.Default;
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
// Add a remover level
dstManager.AddComponentData(entity, new ItemTypeData
{
itemType = itemType,
Entity = conversionSystem.GetPrimaryEntity(this)
});
}
}
Then in MonoBehaviour
public void Spawn()
{
var query = entityManager.CreateEntityQuery(typeof(ItemTypeData)).ToComponentDataArray<ItemTypeData>(Allocator.Temp);
var entity = query.Where((x) => x.itemType == ItemTypeToInstantiate).First().Entity;
var instance = entityManager.Instantiate(entity);
// Transform the instantiated entity in world space
entityManager.SetComponentData(instance, new Translation { Value = transform.position });
entityManager.SetComponentData(instance, new Rotation { Value = transform.rotation });
}
It works and it is indeed much faster than creating a Pool system.
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)
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