ECS. Bug or feature.

Hello! I discovered some strangeness. If we instantiate the object in the old-fashioned way (UnityEngine.Object) and it has the necessary components, then ComponentSystem correctly finds the corresponding groups of objects (example code below):

public class SomeManager : MonoBehaviour
{
    public GameObject prefab;

    void DoStuff()
    {
        Instantiate(prefab);
    }
}

public class SomeSystem : ComponentSystem
{
    struct SomeGroup
    {
        public ComponentArray<SomeOldComponent> SomeOldComponent;
        public EntityArray              Entity;
        public int                      Length;
    }

    [Inject] SomeGroup someGroup;

    protected override void OnUpdate()
    {
        Debug.Log(someGroup.Length); // <- if "prefab" (higher) has Some Component then Length more than 0
    }
}

If we first instantiate an object and then add a component to it, then ComponentSystem does not see this object:

public class SomeManager : MonoBehaviour
{
    public GameObject prefab;

    void DoStuff()
    {
        GameObject tmp = Instantiate(prefab);
        tmp.AddComponent<SomeOldComponent>();
    }
}

public class SomeSystem : ComponentSystem
{
    struct SomeGroup
    {
        public ComponentArray<SomeOldComponent> SomeOldComponent;
        public EntityArray              Entity;
        public int                      Length;
    }

    [Inject] SomeGroup someGroup;

    protected override void OnUpdate()
    {
        Debug.Log(someGroup.Length); // <- if "prefab" (higher) hasn't Some Component, and we add them after Instantiate, then Length equal 0
    }
}

There is no auto sync. You have to add the components to the entity as well through the EntityManager

Then, with Instantiate, the objects would not be added to the group, which means that with Instantiate (old-school style) there is a synchronization point and can also be in the old-school addition of the component. And I advise you to pay attention to the code - it does not use ECS instantiate.

The GameObjectEntity Component adds all ECS Components of that GameObject at Start/Awake Method (don’t know exactly) to the ECS.

You can take a look into this component by clicking the GameObjectEntity in the inspector. It will show up the GameObjectEntity.cs script in the Project view.

You did not understand a bit (or I incorrectly explained), I’m aware of how GameObjectEntity works (adds components to EntityManager in OnEnable), I’m just talking about why Unity did not add an event to add a component that would catch the change in the composition of the object’s components for hybrid approach.

We are planning to add support for it. But it requires new events to be exposed in the core engine.

1 Like

Hopefully I can see the components of Game Object at Inspector and the components of Entity at Entity Debugger sync seamlessly soon.

That’s all I wanted to hear! Look forward to. Thank you! And we are waiting for the release of 2018.1 :slight_smile: :slight_smile: As I suspect, b14 can not wait until the release (you said that in the new version of the ECS will be “out of the box” and will not need a “specific version”) :slight_smile: