Components created with [GenerateAuthoringComponent] are in another world

Hello,

I have a Component with the [GenerateAuthoringComponent] attribute. This component is assigned to an empty GameObject with the Convert To Entity script. The component holds a single integer field, called integerField, which is editable from the Editor.

I have a System that is supposed to read that integer on startup. However, I can’t seem to able to access that integer. I’m using GetSingleton<MyComponent> but I get an error on startup saying that there are no instances of such component.

// find the component which stores my integer (editable on Editor, since it's an authoring component)
EntityQuery query = this.World.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<MyComponent>());

// get the integer field we're interested in
int theValue = query.GetSingleton<MyComponent>().integerField;

Specifically, the exception says “GetSingletonEntity() requires that exactly one exists but there are 0”.

I believe my problem is that the Component that I’m trying to access to read that integer “lives” in the DefaultGameObjectInjectionWorld world, since it was created using [GenerateAuthoringComponent], and my system “lives” in a different world (the default one?), but I can’t seem to overcome this.

Any ideas? Thank you.

Are you trying to access it on OnCreate? Because OnCreate will be called before any scene is loaded.

What you want to do is add an RequireSingletonForUpdate(query) in OnCreate, and use the OnStartRunning to get the integerField.

1 Like

That worked perfectly, @brunocoimbra . Thank you!

1 Like