How reference an empty prefab in baker?

Hello, I’m currently working on a FPS game.
Each weapon has a scriptatble object, with default weapon modifiers specified ( scope, silencer…)
It is possible from the scriptable object to specify or not default modifiers in the form of prefab, the problem is that if you do not put a modifier it will be nullptr during baking which leads to errors, is it possible to reserve a memory space for a prefab to give it a reference to a prefab during runtime ?



image

Simply dont assign a PrefabReference in your baker if the given attachement is null. You can still change the PrefabReference at runtime regardless of wether or not its valid.

Thanks for your answer, unfortunately I already tried and it didn’t work


This doesn’t work either :

scope = d.scope.prefab != null ? new EntityPrefabReference(d.scope.prefab) : default

This probably gives you a null reference exception because the modifier (scope) is null and not the prefab of said modifier (scope.prefab).

Try replacing

scope = d.scope.prefab != null ? new EntityPrefabReference(d.scope.prefab) : default

with

scope = d.scope != null ? new EntityPrefabReference(d.scope.prefab) : default

better yet wrap it in a local function

AddComponent(entity. new ModifiersComponent
{
     scope = GetPrefabRef(d.scope),
     ... 
     etc.
});

EntityPrefabReference GetPrefabRef(*Your ScriptableObject type* modifierData)
{
     return modifierData == null ? default : new EntityPrefabReference(modifierData.prefab);
}
2 Likes

It works, thank you very much :blush: