Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to set a singleton in an Authoring Component and then Get it. (Solved)

Discussion in 'Entity Component System' started by Kowarenai, Mar 18, 2021.

  1. Kowarenai

    Kowarenai

    Joined:
    May 27, 2019
    Posts:
    14
    I'm trying to create some singletons using the conversion workflow as configuration data, and read from them in different systems. I've tried everything I can think of, but the entity query in the system just can't find anything.

    This is my code in the authoring to do the conversion.

    Code (CSharp):
    1.         public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    2.         {
    3.             dstManager.SetComponentData(entity, new CameraConfigSingleton {defaultFOV = Camera.main.fieldOfView} );
    4.         }
    I dragged the authoring monobehaviour onto an empty gameobject and converted, looks as expected.



    Yet when I try to call GetSingleton, or do any sort of query for the entity it can't find it.

    Code (CSharp):
    1.         protected override void OnCreate()
    2.         {
    3.             startingZoom = GetSingleton<CameraConfigSingleton>().defaultFOV;
    4.         }
    "InvalidOperationException: GetSingleton<DataComponents.Singletons.CameraConfigSingleton>() requires that exactly one DataComponents.Singletons.CameraConfigSingleton exist that match this query, but there are 0."

    I've tried all sorts to get it to work, and it even looks similar to what i've seen in the samples repo but, I just can't figure out whats wrong with it. Any help is appreciated!
     
  2. Cell-i-Zenit

    Cell-i-Zenit

    Joined:
    Mar 11, 2016
    Posts:
    288
    Put some debug.log messages down and see what executes earlier. I guess the OnCreate happens earlier then the monobehaviour conversion
     
    Kowarenai likes this.
  3. Kowarenai

    Kowarenai

    Joined:
    May 27, 2019
    Posts:
    14
    That was my guess, but I tried waiting to get the singleton until I press a key in Update just to test it, but no luck.
     
  4. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    677
    OnCreate should not query for entities, for any auto-created system it will happen before any conversion or scene is loaded. Use RequirieSingletonForUpdate and then query the singleton on the OnStartRunnning if you want it to be a one-time thing
     
    Kowarenai likes this.
  5. Kowarenai

    Kowarenai

    Joined:
    May 27, 2019
    Posts:
    14
    Thanks so much, that fixed it!
     
  6. Kowarenai

    Kowarenai

    Joined:
    May 27, 2019
    Posts:
    14
    Looks like you were correct about the OnCreate happening earlier, must have made a mistake when i tried to test for that previously.
     
  7. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,626
    This is not always the case. If you use a default bootstrap 100% but I've found creating all your setting entities etc before systems are created is a very useful pattern for setting management, save files and backwards compatibility.
    Anyway I recently discussed my current lifecycle setup here

    https://forum.unity.com/threads/loading-non-editor-data-to-use-in-systems.1074505/#post-6932256
     
  8. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    677
    Yeah, I was referring to the default bootstrap. For easier code reusability between projects, I tend to always consider the default one as it makes it easier to create standard workflows for other developers too.