ECS error for what should be very simple scripts (error DC0023)

Hi all,

I am new to Unity and I only code as a hobby, so please be very descriptive about any help you provide: I am by no means a professional programmer.

I have been trying for weeks now to put together the first few bricks of a video game I am making on my own.
Since I am new to Unity I thought I would get straight into good habits by coding using Hybrid or Pure ECS (Hybrid seems to be a lot more flexible and accessible). After going through a significant amount of Unity documentation (this stuff can be seriously hard to read and not user-friendly unfortunately), forums and youtube tutorials and trying things out in Unity, I came to the realization that ECS coding has been constantly changing for at least 2-3 years and that a good 90% of the help material put online is either deprecated or on the way to be deprecated (e.g., JobSystem, ComponentSystem). Even more frustration came when I starting finding some incoherences in the official Unity ECS documentations regarding the number of components taken by Entities.ForEach (6? 8? who knows…). I don’t blame Unity, this is just factual but it is frustrating, especially far a new user such as myself :frowning:

Ok, long story short, my adaptation of a (very simple in principle) Hybrid ECS code from a youtube tuto (

) does not work and I have tried so many things now that I do not know what to do anymore… Help would be very much appreciated!
All details are below:

The error I get:
error DC0023: Entities.ForEach uses managed IComponentData MovementSpeed&. This is only supported when using .WithoutBurst() and .Run().

My loaded packages (the key ones only):

  • Entities
  • Hybrid Renderer
  • Jobs
  • Burst

My Scene content:

  • Main Camera (GameObject)
  • Directional Light (GameObject)
  • Plane/Ground (GameObject converted into an Entity at runtime using the built-in script set on Convert&Destroy)
  • Cube/Player (GameObject converted into an Entity at runtime using the built-in script set on Convert&Destroy)

The following ECS Components have been added to the Cube (Hybrid ECS so these are MonoBehaviour, I will also try in Pure ECS tonight):

using UnityEngine;

namespace HybridECS.Components
{
    public class MovementSpeed : MonoBehaviour
    {
        public float value;
    }
}

And here is my ECS System (using SystemBase, since the other ways are to be deprecated soon):

using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;    // to use math
using Unity.Transforms;     // to use Translation (= Transform in ECS???)
using UnityEngine;

namespace HybridECS.Systems
{
    public class PlayerMovementSystem : SystemBase // ComponentSystem is being deprecated
    {
        protected override void OnUpdate()
        {
            // Local variables used in Entities.ForEach should be defined beforehand
            float deltaTime = Time.DeltaTime;

            // filters the entities based on theirs associated components, which will be included in/affected by this ECS System
            // can take up to 8 types of components
            // add the "ref" keyword to read&write the component; add the "in" keyword to read-only the component                       
            Entities.ForEach((ref Translation translation, in Rotation rotation, in HybridECS.Components.MovementSpeed speed) => {
               
                // write the system behaviour here, example of movement script:
                translation.Value.x += speed.value * deltaTime;

            }).Schedule();
        }
    }
}

If I cannot figure out how to do things in ECS I might just do everything using Monobehaviours, but it would be very disappointing for me who enjoys coding not just to make my own small video games but also to simply keep learning new things and ways of thinking.

Thanks very much in advance for your help!

The error message is slightly off here. Instead of IComponentData, it should say MonoBehaviour. But it is otherwise correct. You are missing .WithoutBurst() in your Entities.ForEach fluent chain. By default, Entities.ForEach will compile with Burst, but this only works when using “pure ECS” or unmanaged struct IComponentData types.

The other thing that has changed is the argument of “Hybrid” vs “Pure”. Really it is a spectrum that offers a performance vs convenience tradeoff. The convenience gains being things that are implemented in classic Unity but not in DOTS.

Thanks for the help DreamingImLatios, unfortunately I just tried and it returns another error now:

error DC0011: Every Entities.ForEach statement needs to end with a .Schedule(), .ScheduleParallel() or .Run() invocation.

are you posting all the relevant code? the error message warns about a PlayerPosition IComponentData but none of the code snippets you posted contain that

The error message is telling you exactly what is wrong and what you need to do. If you don’t understand it, post code.

Hi thelebaron, yes all the relevant code is there. You are absolutely right though: I forgot to remove that PlayerPosition IComponentData from the ForEach(). I also removed the PlayerInputComponent and PlayerInputSystem to make things easier (updated code at the top), but it does not fix my error and I am still getting an error:

error DC0023: Entities.ForEach uses managed IComponentData MovementSpeed&. This is only supported when using .WithoutBurst() and .Run().