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 Problem When Istantiating an Entity from an ISystem System

Discussion in 'Entity Component System' started by KaykyL, Aug 1, 2023.

  1. KaykyL

    KaykyL

    Joined:
    Mar 2, 2022
    Posts:
    3
    Hello There!

    I'm new in Unity ECS, and I have a problem when instantiating an entity from an ISystem System.

    The problem is relationed with the command buffer singleton. Every time I try to access it from the OnCreate() of my system, I get an error on console, informing that the singleton doesn't exist:

    InvalidOperationException: System.InvalidOperationException: GetSingleton<{0}>() requires that exactly one entity exists that match this query, but there are none. Are you missing a call to RequireForUpdate<T>()? You could also use TryGetSingleton<T>()
    This Exception was thrown from a function compiled with Burst, which has limited exception support.


    This is the code of my OnCreate function:

    Code (CSharp):
    1.  
    2.     [BurstCompile]
    3.     public void OnCreate(ref SystemState state)
    4.     {
    5.         state.Dependency.Complete();
    6.  
    7.         var spawner = SystemAPI.GetSingleton<PlayerSpawner>();
    8.         var entityCommandBufferSingleton = SystemAPI.GetSingleton<BeginSimulationEntityCommandBufferSystem.Singleton>();
    9.         var entityCommandBuffer = entityCommandBufferSingleton.CreateCommandBuffer(state.WorldUnmanaged);
    10.         var random = SystemAPI.GetSingletonRW<RandomComponent>();
    11.  
    12.         new SpawnJob()
    13.         {
    14.             playerSpawner = spawner,
    15.             commandBuffer = entityCommandBuffer,
    16.             random = random,
    17.         }.Schedule(state.Dependency);
    18.     }
    I already tried call the Complete() method within the Dependency, to complete the previous jobs, making shure the CommandBuffer Singleton is already created. Did I miss something?

    Thanks!
     
  2. xindexer2

    xindexer2

    Joined:
    Nov 30, 2013
    Posts:
    78
    You don’t need to complete the dependency, you need to complete the job,


    Code (CSharp):
    1.         new SpawnJob()
    2.         {
    3.             playerSpawner = spawner,
    4.             commandBuffer = entityCommandBuffer,
    5.             random = random,
    6.         }.Schedule(state.Dependency).complete();
     
    KaykyL likes this.
  3. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    274
    This is all just from the framework producing an order for calling the OnCreate for every system. If you do not define your own ordering constraints with CreateBefore and CreateAfter, there is a possibility that the creation order will end up with some systems trying to use singletons that have not been created yet. You need to ensure that the system is created after BeginSimulationEntityCommandBufferSystem to ensure that the singleton BeginSimulationEntityCommandBufferSystem.Singleton is available for use. Adding [CreateAfter(typeof(BeginSimulationEntityCommandBufferSystem))] ensures that your OnCreate will be called after the OnCreate of BeginSimulationEntityCommandBufferSystem. You would also want to add a CreateAfter on the system that targets the system that creates the RandomComponent singleton so that doesn't throw an exception as well.

    Additionally, when you schedule SpawnJob, you should either assign the result back to state.Dependency or, if this is IJobEntity or IJobChunk, omit the state.Dependency argument (this causes the source generated version of OnCreate to perform the assignment automatically) to ensure that the system's dependency is correct when exiting OnCreate.
     
    KaykyL likes this.
  4. KaykyL

    KaykyL

    Joined:
    Mar 2, 2022
    Posts:
    3
    Hello, thanks for the help!

    I added the CreateAfter, and the error doesn't happens more. But now the same error happens when getting Singleton of the other two Singletons: GetSingleton<PlayerSpawner>() and GetSingleton<RandomComponent>();

    Is there any way to use CreateAfter the creation of these components?
     
  5. KaykyL

    KaykyL

    Joined:
    Mar 2, 2022
    Posts:
    3
    Allright, thank you for the advice!
    It will certainly avoid future problemas with dependecys.