#Why does Instantiated entities show up as Invalid? And why can't it be destroyed?

1 messages · Page 1 of 1 (latest)

unborn drift
#

I instantiated an entity using this entity command buffer line(in an IJobEntity) ECB.Instantiate(entityToSpawn);, and I can see the entity in the scene, but on the runtime inspector, my reference to the instantiated entity shows up as Invalid (Entity {-1:-22}). And when I try to destroy the same entity, I get this error thrown in the inspector:

ArgumentException: System.InvalidOperationException: Deferred Entity Unity.Entities.Entity was created by a different command buffer. Deferred Entities can only be used by the command buffer that created them.
This Exception was thrown from a function compiled with Burst, which has limited exception support.

And the entity isn't destroyed. What could've gone wrong?

civic yew
#

so what you get instead is deferred entity

#

which is only valid for same ecb that created it

#

just like the error says

#

consider this deferred entity as just a temporary key

unborn drift
#

But a previous project I worked on never had this issue. The entity was spawned and referenced correctly(not as an invalid entity). And I could destroy it afterwards.

#

Anyways, how else can an entity be instantiated/destroyed in a job without ecb executed every update?

radiant crown
#

If you spawn entity in job and want to store reference you should set it with the same ecb and it will be resolved correctly
var ent = ecb.instantiate(prefab);
ecb.setcomponent(otherEnt, new() { entity = ent });

unborn drift
#

That’s awesome! Thanks it worked!!
I was initially setting the reference directly because I was already getting the component as a reference using the ref keyword in the job.
SetComponent made it work

ancient plinth
#

But a previous project I worked on never had this issue.
Honestly without 2 versions of the code to compare, this claim is meaningless to us outsiders.

#

Anw, the fact is ECB is just a collection of commands. It records the command you invoke but do nothing to actualize the command.

#

Only when ECB is played back by a system, recorded commands will be actualized through the EntityManager.