#Creating data only entity prefabs during baking

1 messages · Page 1 of 1 (latest)

torn hare
#

This was going to be a post about why EntityManager.Instantiate returns Entity.Null when trying to instantiate prefabs in standalone build but after some more debugging I realized it only returns null for prefabs I tried to create in the baker on my own.

The code I was using in the baker:

Entity containerPrefab = baker.CreateAdditionalEntity(TransformUsageFlags.None, false, _Name + "ContainerItem");
baker.AddComponent<ContainerItemTag>(containerPrefab);
baker.AddComponent<Prefab>(containerPrefab);
baker.AddBuffer<LinkedEntityGroup>(containerPrefab);
baker.AddComponent(containerPrefab, new ItemDataComponent
{
    Icon = _Icon,
    ItemSize = _ContainerSize
});
baker.AddComponent<ContainerPosition>(containerPrefab);

Calling EntityManager.Instantiate on the prefab works fine in editor but fails by silently returning Entity.Null in a build. If I remove the Prefab and LinkedEntityGroup components before calling instantiate it works again.

I'm suspecting we are not supposed to create our own custom prefab entities? Or am I missing something? My end goal is to have a prefab like entity I can spawn on demand which holds certain properties (in this case a weak reference to a sprite, size in the inventory etc.).

fossil rover
#

original entity LEG will contain your prefab as part of LEG

#

so when you instantiate original entity that gets baked

#

it will instantiate your generated prefab as well

torn hare
#

What does "LEG" stand for?

fossil rover
#

LinkedEntityGroup

#

but this is a bit unrelated to your null issue

#

I'm not sure EntityManager.Instantiate can ever return Null

#

it should definitely just throw

torn hare
#

Ah right. I was not intending both prefabs to get instantiated together. I'm just generating the second one off data fromt he first one. I only added LinkedEntityGroup to it so it would get recognized as a Prefab.

#

Yeah I was suprised it did not complain in any way but stepping through the code it definitely returned Entity.Null

fossil rover
#

what entity was passed to it?

#

maybe it was invalid

torn hare
#

The containerPrefab in the code snippet above. Not during baking but during gameplay. It also worked fine in the editor and only started returning Entity.Null in a game build.

fossil rover
#

yeah, but are you sure it's actually fine in build?

#

I'm leaning more towards your code failing somewhere and passing invalid thing vs EntityManager silently failing

torn hare
#

Setting a breakpoint it had all the needed components on the prefab entity. And I was able to work around the issue by removing the Prefab and LinkedEntityGroup components before instantiating.

EntityManager.RemoveComponent<Prefab>(itemPrefab);
EntityManager.RemoveComponent<LinkedEntityGroup>(itemPrefab);

Entity containerItem = EntityManager.Instantiate(itemPrefab);
fossil rover
#

wait

#

your LEG

#

on your prefab

#

it's invalid

#

don't add it

#

unless you set it up

#

or actually need it

#

prefabs don't need leg

torn hare
#

Oh. Yeah I only ended up adding it so the Entities Hierarchy window would correctly show it as a prefab

#

Yes that worked! You saved me

#

A little strange that the PistolContainerItem in the hierarchy doesn't have the same icon but maybe I was just misinterpreting them

#

But anyway. Everything seems to work fine now!

fossil rover
#

if you want to have multiple entities as part of prefab

#

otherwise you don't need it

#

but if you do want it

#

you need to ensure all entities are in leg

#

and main prefab is first entity in a list