public class PlayerInputSystem : ComponentSystem
{
public struct Data
{
public int Length;
public ComponentArray<NavMeshAgent> Navigation;
public ComponentArray<Player> PlayerTag;
}
[Inject] private Data m_Data;
protected override void OnUpdate()
{
Debug.Log(m_Data.Length);
}
}
And Component (Tag):
namespace GameCode.Components.Player
{
public class Player : MonoBehaviour
{
}
}
I have a GameObject in the scene with NavMeshAgent and Player component. Length in m_Data is always 0.
Why does it not get injected?
Edit:
OK I forgot to add GameObjectEntity But I do not know how to add components at runtime, so other systems will react.
var cubeEntity = cube.AddComponent<GameObjectEntity>().Entity;
entityManager.AddComponentData(cubeEntity, new CubeComponent());
Essentially you need the EntityManager, your entity and the IComponentData you want to add. Then tell the EntityManager to add the IComponentData to your Entity.
If I remember correctly, there was a warning not to add/remove components during OnUpdate as it may stall the execution of your job due to synchronization. But take this all with care, I’m too have to wrap my head around this stuff
I have tried this but I am using a hybrid approach and my Components are MonoBehaviours and I get a compiler error when I pass my Component to AddComponentData. It says that second argument must be non-nullable. Maybe I have to wrap my behaviors.
EDIT:
Ok I had to wrap my component and It works now:
public struct Destination : IComponentData
{
public float3 position;
}
public class DestinationWrapper : ComponentDataWrapper<Destination>
{
}
Yeah… and no Player is a MonoBehaviour, not an IComponentData. ComponentDataArray can only take structs derived from IComponentData.
BUT Unity want us to use IComponentData. As long as not all Unity components (Rigidbodies for example) are not fully supported in ECS, the ComponentArray will fill that gap.
Ok, here I have to correct myself. The warning was for creating new Entities, not components. Haven’t seen warnings about adding/removing components in the docs. I guess it may also be recommended to do so, so you can add components as filters (like a DeadComponent).
@Micz84 Did you say you managed to add/remove Monobehaviour components live during runtime?
I’ve been trying to add Monobehaviour components during runtime using gameObject.AddComponent but it seems like GameObjectEntity only adds Monobehaviour components that are currently on the game object when adding GameObjectEntity.