Meshes in Monobehaviour are working, in JobComponentSystem not, with same code.

Hi folks,

Strange thing! I use the same code, one in Monobehavior and the other in JobComponentSystem.

Monobehavior works like a charm, but not in the other.

The error message I get:
: A component with type:RenderMesh has not been added to the entity.
Unity.Entities.EntityComponentStore.AssertEntityHasComponent (Unity.Entities.Entity entity, Unity.Entities.ComponentType componentType) (at Library/PackageCache/com.unity.entities@0.11.2-preview.1/Unity.Entities/EntityComponentStoreDebug.cs:271)

Unity.Entities.DefaultWorldInitialization:AddSystemsToRootLevelSystemGroups(World, Type[ ]) (at Library/PackageCache/com.unity.entities@0.11.2-preview.1/Unity.Entities.Hybrid/Injection/DefaultWorldInitialization.cs:127)
Unity.Entities.DefaultWorldInitialization:Initialize(String, Boolean) (at Library/PackageCache/com.unity.entities@0.11.2-preview.1/Unity.Entities.Hybrid/Injection/DefaultWorldInitialization.cs:106)
Unity.Entities.AutomaticWorldBootstrap:Initialize() (at Library/PackageCache/com.unity.entities@0.11.2-preview.1/Unity.Entities.Hybrid/Injection/AutomaticWorldBootstrap.cs:15)

Here is my code:

public class Management : JobComponentSystem
{
    public bool updatetrue = true;
    public List<SList> sList = new List<SList>();
    private EntityManager sEntityManager;
    Mesh sMesh;
    Material sGlow1;
    Material sGlow2;
   protected override void OnCreate()
   {
        sMesh = Resources.Load<Mesh>("Meshs/SMesh");
        sGlow1 = Resources.Load<Material>("Materials/SGlow");
        sGlow2 = Resources.Load<Material>("Materials/PinkGlow");

       // Fill list with Query.
  // Here is some code.

        sEntityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
       foreach (SList singleship in sList)
        {
            CreateSEntity(singles);
       }
    }

    void CreateSEntity(SList sShip)
    {
        Entity sEntity = sEntityManager.CreateEntity();
#if UNITY_EDITOR
        sEntityManager.SetName(sEntity, "S_Player: " + singleS.playerName + "/" + singleS.shipId);
#endif
        sEntityManager.AddComponentData(sEntity, new PlayerComponent
        {           playerName = singleS.playerName       });
        sEntityManager.AddComponentData(sEntity, new SEntityComponent
        {            sId = singleS.sId        });
       sEntityManager.AddComponentData(sEntity, new Translation
        {     Value = new float3(singleS.currentX, singleS.currentY, singleS.currentZ)        });

        myRandom = Random.Range(0f, 10f);
        if (myRandom > 5)
        {
// This causes the error depending on random
     sEntityManager.SetSharedComponentData(sEntity, new RenderMesh
            { mesh = sMesh, material = sGlow1 });
        }
        else
        {
// This causes the error depending on random
            sEntityManager.SetSharedComponentData(sEntity, new RenderMesh
            { mesh = sMesh, material = sGlow2 });
        }
        sEntityManager.AddComponentData(sEntity, new LocalToWorld { });
        sEntityManager.AddComponentData(sEntity, new RenderBounds { });
        sEntityManager.AddComponentData(sEntity, new Rotation { });
  }

Somebody has an idea, please?
Thanks
Slarti

It looks like you’re trying to create entities in OnCreate(), if this system is created during the normal bootstrap process, the world may not be initialized.

You might want to change this to create those entities only on the first update. You should also investigate using SystemBase instead of JobComponentSystem, as the latter is being deprecated in favor of the former.

@recursive thank you for your reply.

I added base.OnCreate() - no change.
I moved the routine to OnUpdate - no change.

I have another JobComponentSystem and it is set up like this and it works OnCreate.

scratches head

OMG!!! :roll_eyes:
I remembered, when I set up the monobehavior script, that something didn’t work (so not exactly the same). Instead of
SetSharedComponentData
it has to be
AddComponentData
Now it works. Thanks @recursive for your thoughts.
Cheers,
Slarti