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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Convert to Entity & Inject GameObject, Spawns a 2nd GameObject that doesn't do anything

Discussion in 'Entity Component System' started by ElSheriff, May 27, 2020.

  1. ElSheriff

    ElSheriff

    Joined:
    Apr 12, 2014
    Posts:
    9
    Hi,

    Bellow you can find my 2 scripts, I am also am using a convert to entity, with convert and inject game object. The attached image is the game object I am controlling with a separate script and the 2nd game object that doesn't move but shows up in the entity debugger.
    I don't know why this 2nd game object is created.

    My Player to Entity Converter is:
    Code (CSharp):
    1. public class PlayerToEntityConversion : MonoBehaviour, IConvertGameObjectToEntity
    2. {
    3.     public float healthValue = 1f;
    4.  
    5.  
    6.     public void Convert(Entity entity, EntityManager manager, GameObjectConversionSystem conversionSystem)
    7.     {
    8.         manager.AddComponent(entity, typeof(PlayerTag));
    9.  
    10.         Health health = new Health { Value = healthValue };
    11.         manager.AddComponentData(entity, health);
    12.     }
    13. }
    My Player Tag component:
    Code (CSharp):
    1. [Serializable]
    2. [GenerateAuthoringComponent]
    3. public struct PlayerTag :IComponentData
    4. {
    5. }
     

    Attached Files:

  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,993
    That's what Convert and Inject usually does. Is there a reason you can't use Convert and Destroy?
     
  3. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    825
    If you are using inject with something that has a renderer, for now it results in the awkward situation where an entity is created with the converted RenderMesh and your original MeshRenderer still hangs around. You could simple disable it somewhere in your code. Another option would be to create a custom ConversionSystem to remove the RenderMesh after its been added(tbh I cant remember what the result is if you attempt to remove a component that is automatically converted inside the Convert interface but this might be something to try too).

    note - its not a second gameobject(unless theres more to this that has not been mentioned), its your original gameobject that you setup in the scene.
    It all depends on what you want from your end result.
     
  4. ElSheriff

    ElSheriff

    Joined:
    Apr 12, 2014
    Posts:
    9
    Thank you!
    I am trying to get, a setup similar to Unity's AngryDOTS project. As I will be using a rig that I can only use with MonoBehaviors and cannot convert directly to an entity. I didn't know injection will do this. I am now doing rendering on a child without conversion and it works fine.