In my project i mostly use ConvertAndDestroy and Companion workflow. But after i understood performance problem of companion system i started to try switch to ConvertAndInject. Also whole time in my project my main camera had ConvertAndInject, so all worked well a very long time.
But then after switching to new unity version (2020.3.0f1) i started to see a strange error
UnityEngine.Object:smile:estroyImmediate (UnityEngine.Object)```
After some dancing with components i came to a conclusion that if camera have Volume (URP) component then error appears, but if volume component are in separated gameobject then there is no error. Ok, strange, but fixed.
But few days ago i tryed to make my sprite gameobject be ConvertAndInject. And by some reason DOTS still creates sprite gameobject hided clone and add companion link. So after press play i have 2 sprites. One in dots world, hided and linked with companion link, and another one still exists in scene, not hided.
I tryed to open my test little project that uses dots and there is no such a problem (same 2020.3.0f1 was used).
I just have no clue, what causes that behaviour. But then i thought that SpriteRenderer has built-in GameObjectConversionSystem that do AddHybridComponent, and that how we can have all this companion stuff with sprite renderer component. But if your gameobject have, for example, sort group it won't be added as hybrid component, so you need to define your own gameobject conversion system. I have few, and one of them is Light2DGameObjectConversionSystem which just do AddHybrid(2DLight) to entity. And i was so surprised when figured out that when this system works ConvertAndInject do his strange behaviour and spawns additional gameobject with Light2D, but when this system Disabled, then gameobject with Light2D convetrs perfectyly, just being injected as expected.
As i mentioned before i have no clue, but maybe some of you have faced this problem.
Solvation:
I figured out that this behaviour caused when some component used in GameObjectConversionSystem. Seems like HybridRenderer contains system to add SpriteRenderer as HybridComponent. Code should looks like:
```csharp
Entities.ForEach((SpriteRenderer spriteRenderer) =>
{
AddHybridComponent(spriteRenderer);
});
But also it seems like this code does not take into account conversion mode. So a have deleted HybrydRenderer package and have wrote abstract GameObjectConversionSystem:
public abstract class AddHybridComponentConversionSystem<TComponent> : GameObjectConversionSystem
where TComponent : Component
{
protected override void OnUpdate()
{
Entities
.WithNone<ConvertToEntity>()
.ForEach((TComponent component) =>
{
AddHybridComponent(component);
});
Entities
.ForEach((TComponent component, ConvertToEntity convertToEntity) =>
{
if(convertToEntity.ConversionMode == ConvertToEntity.Mode.ConvertAndDestroy)
AddHybridComponent(component);
});
}
}
So now, if i use ConvertAndDestroy or even have no ConvertToEntity mono GameObjectConversionSystem will add components in hybrid way, but if i use ConvertAndInject then system wonāt do anything and entity will just have ComponentObjects as expected.