The ECS example in the docs doesn't work

So I did the startup tutorial in the Entities documentation v 1.2

https://docs.unity3d.com/Packages/com.unity.entities@1.2/manual/ecs-workflow-example.html

And it doesn’t work.

  • I create a scene.
  • I create a subscene. It doesn’t tell you if you have to add it or not because it’s supposed to be added automatically but it doesn’t. Do I add it myself additively?
  • Create a spawner struct
public struct Spawner : IComponentData
{
    public Entity Prefab;
    public float3 SpawnPosition;
    public float NextSpawnTime;
    public float SpawnRate;
}
  • In the subscene add a Spawner object with the following SpawnerAuthoring component
class SpawnerAuthoring : MonoBehaviour
{
    public GameObject Prefab;
    public float SpawnRate;
}

class SpawnerBaker : Baker<SpawnerAuthoring>
{
    public override void Bake(SpawnerAuthoring authoring)
    {
        var entity = GetEntity(TransformUsageFlags.None);
        AddComponent(entity, new Spawner
        {
            // By default, each authoring GameObject turns into an Entity.
            // Given a GameObject (or authoring component), GetEntity looks up the resulting Entity.
            Prefab = GetEntity(authoring.Prefab, TransformUsageFlags.Dynamic),
            SpawnPosition = authoring.transform.position,
            NextSpawnTime = 0.0f,
            SpawnRate = authoring.SpawnRate
        });
    }
}
  • Create a prefab (cube) and attach to a GO with this component in the subscene. Set spawn rate to 2.
  • We already have problems. it says

In the Entities Hierarchy window, select the Spawner entity. The Entities Hierarchy window displays both GameObjects and entities. To distinguish between the two, entities are indicated by a hexagon icon (). Well the Hierarchy DOESN’T show any entity. Should I hit play? no entity shows up there but Unity Logging.
2. In the Inspector for the Spawner entity, … (OK. Here I can’t do anything else, because no entity is shown in the inspector)

[BurstCompile]
public partial struct SpawnerSystem : ISystem
{
    public void OnCreate(ref SystemState state) { }

    public void OnDestroy(ref SystemState state) { }

    [BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        // Queries for all Spawner components. Uses RefRW because this system wants
        // to read from and write to the component. If the system only needed read-only
        // access, it would use RefRO instead.
        foreach (RefRW<Spawner> spawner in SystemAPI.Query<RefRW<Spawner>>())
        {
            ProcessSpawner(ref state, spawner);
        }
    }

    private void ProcessSpawner(ref SystemState state, RefRW<Spawner> spawner)
    {
        // If the next spawn time has passed.
        if (spawner.ValueRO.NextSpawnTime < SystemAPI.Time.ElapsedTime)
        {
            // Spawns a new entity and positions it at the spawner.
            Entity newEntity = state.EntityManager.Instantiate(spawner.ValueRO.Prefab);
            // LocalPosition.FromPosition returns a Transform initialized with the given position.
            state.EntityManager.SetComponentData(newEntity, LocalTransform.FromPosition(spawner.ValueRO.SpawnPosition));

            // Resets the next spawn time.
            spawner.ValueRW.NextSpawnTime = (float)SystemAPI.Time.ElapsedTime + spawner.ValueRO.SpawnRate;
        }
    }
}
  • When hitting play it will start spawning objects. Well, it doesn’t. And OnUpdate is not even called.

So yeah, the tutorial is not working. I can’t get it to work because I don’t know about ECS yet. Anyone who can help?

1 Like

General

Start the project with a URP or HDRP template and add the Entities Graphics package if you want to see the spawned boxes. This is only mentioned later in the tutorial.

Creating the subscene

Create scene

Screenshot 2024-08-20 032637

Right-click, create empty subscene, fill in a name for it (don’t change location, it’s fine where it is)

Screenshot 2024-08-20 032805

Subscene is automatically added via a new GameObject in the scene that has a SubScene component with the appropriate Scene reference (Scene Asset property points to the created subscene)

Screenshot 2024-08-20 032752

image

Object creation

Make sure the object is actually nested inside the subscene

image

Entity Inspector

The subscene can either be open for editing (Entities Hierarchy shows GameObjects, only in Authoring and Mixed mode) or closed (Entities Hierarchy shows the baked entities, in Mixed and Runtime). The subscene can be closed by unchecking the subscene (thus closing it) either in the normal scene Hierarchy view or the Entities Hierarchy.

Inspecting a GameObject

Requires open (checked) subscene

image

Bottom of the Inspector view for the GameObject shows “Entity Baking Preview” - to view the components for the entity in the current editor version of the world.

image

You can also view “Converted Scene” entries to see inspect the entity data used when baking for that specific subscene (a prefab for instance could be baked into multiple subscenes). Note that in “Converted Scene” there are extra baking components for baking that won’t be present in the actualized subscene used in a game.

image

Inspecting an Entity

Requires closed (unchecked) subscene

image

image

Entities can be inspected in mixed / runtime mode to view associated components.

etc.

Possible issues:

  • Bugged subscene creation or unclear about what a subscene looks like in the editor

  • Doesn’t bring up URP/HDRP + Entities Graphics requirement until 4th page

For what it’s worth, the official samples repo has ready-made samples to take a gander at.

1 Like

THANKS. That’s a good tutorial.

It lacks a lot of info. I created the subscene in the project view, I don’t know why I did that, but I could so I did it. Then added it (additive) to the main one.
That’s when everything went wrong.

Also the additional comments helped. Thanks a lot.

Update. I updated the example, did exactly as you did. I have everything setup. OnUpdate() on the system is not called. It just doesn’t create anything. :woman_shrugging: … oh well…

Perhaps send the project sans Logs/Library folders for others to diagnose. The process seems to work correctly on my end.

I created an empty project and it worked. It looked completely different.
So the problem must be in the project I was in, which used Entities intensively. I didn’t know having other stuff using ECS in other scenes could affect this standalone scene!

Scene content shouldn’t interfere by itself, but the issue could be explained if there are any systems or other external code that do things like change which systems run, disable/destroy entities, or mess with the world time (since the spawner system uses target elapsed time instead of counting down). If the project uses Netcode for Entities, perhaps there’s something strange like the server world alone has the subscene loaded and thus spawns “invisible” objects (no presentation for server worlds).

If you still care to try diagnosing the non-trivial project:

The Entities Hierarchy view can be used to see if the spawner entity is there at all and if the spawner component is there, and whether the entity itself is disabled (which prevents it from being included in queries by default) or not. Also check that the prefab is present with a blue label and has appropriate components for Entities Graphics, like RenderMeshArray.

The system inspector shows systems in a given world. Ensure the spawner system is present. Clicking on the system makes the Inspector show info on the system, and you can view query results in the “Relationships” tab, which you can use to see if your spawner entity is being picked up by the query.

Somewhat a side note, to see runtime entities in the Scene view, make sure Preferences > Entities > Baking > Scene View Mode is set to runtime data, otherwise it will just be displaying the authoring representation.