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?