How to add variable Type component to entity?

Hi i add same type component to few entities when entities are created.
in my work, add component with switch is fine but i guess its not good performance so i wanna make switch statement before use for statement

This is my origin code

                for (int i = 0; i < Prefabs.lenght; i++)
                {
                    Entity instance = CommandBuffer.Instantiate(index, prefabSpawner.Prefab);
                    switch (prefabSpawner.unitType)
                    {
                        case ScriptType.ECS:
                            CommandBuffer.AddComponent(index, instance, new Player());
                            break;
                        case ScriptType.JECS:
                            CommandBuffer.AddComponent(index, instance, new Enemy());
                            break;
                        case ScriptType.JECS2:
                            CommandBuffer.AddComponent(index, instance, new Unit());
                            break;
                        default:
                            break;
                    }
                }

and i wanna make concept like this

                Type scripttype = typeof(None);
                switch (prefabSpawner.unitType)
                {
                    case UnitType.Player:
                        scripttype = typeof(Player);
                        break;
                    case UnitType.Enemy:
                        scripttype = typeof(Enemy);
                        break;
                    case UnitType.Unit:
                        scripttype = typeof(Unit);
                        break;
                    default:
                        break;
                }

                for (int i = 0; i < Prefabs.lenght; i++)
                {
                    Entity instance = CommandBuffer.Instantiate(index, prefabSpawner.Prefab);
                    CommandBuffer.AddComponent(index, instance, scripttype);
                }

but I got error that

The type ‘Type’ must be a non-nullable value type in order to use it as parameter ‘T’ in the generic type or method ‘EntityCommandBuffer.Concurrent.AddComponent(int, Entity, T)’

How can i fix it?

You need to pass the type into a generic method and then call instantiate from there.
Something like this:

    void Instantiate<T>() where T : struct, IComponentData {
      for (int i = 0; i < Prefabs.lenght; i++) {
        Entity instance = CommandBuffer.Instantiate(index, prefabSpawner.Prefab);
        CommandBuffer.AddComponent(index, instance, default(T));
      }
    }

and then your switch or whatever will look like this:

      switch (prefabSpawner.unitType) {
        case UnitType.Player:
          Instantiate<Player>();
          break;
        case UnitType.Enemy:
          Instantiate<Enemy>();
          break;
        case UnitType.Unit:
          Instantiate<Unit>();
          break;
        default:
          break;
      }

This is what i need it

Thank you.:slight_smile: