Component System OnUpdate Running Unintentionally in Other Scenes (Including New Ones)

230 views Asked by At

I have a class that inherits from ComponentSystem in ECS and for some reason the OnUpdate function is being called unintentionally in every scene. Even if I create a new blank scene, the loop is running (and throwing errors).

How can I fix this?

Code below.

    public class TransportSpriteSystem : ComponentSystem
    {
        //Updates the sprites for transport movement
        public static float timer = 0f;
        LogisticTransportConnection target;
     
        protected override void OnUpdate()
        {
            if (!LogisticManager.I.useEntities)
                return;
            if (timer >= LogisticManager.I.tickRate)
            {
                
                Entities.ForEach((Entity entity, ref TransportSpriteComponent sprite) =>
                {
                    target = LogisticTransportConnection.All[sprite.connectionId];
                    int newId = target.TransportQueueIds[sprite.spriteIndex];
                    if (target.line.isVisible && sprite.lastId != newId)
                    {
                        EntityManager.SetSharedComponentData(entity, InventoryManager.I.itemRenderMeshes[newId]);
                        sprite.lastId = newId;
                    }
                });
                timer = 0f;
                
            }
            else
            {
                timer += Time.DeltaTime;
            }
        }
    }
     
     
    public class TransportSpriteSystemMovement : SystemBase
    {
        //Responsible for smoothly moving the sprites
        protected override void OnUpdate()
        {
            GameManager.stopwatch2.Restart();
     
            float lerpVal = TransportSpriteSystem.timer / LogisticManager.I.tickRate;
     
            Entities.ForEach((ref TransportSpriteComponent sprite, ref TransportSpriteComponentCanMove canMove, ref Translation translation) =>
            {
                if (canMove.canMove)
                    translation.Value = Vector3.Lerp(sprite.start, sprite.end, lerpVal);
            }).ScheduleParallel();
     
            GameManager.stopwatch2.Stop();
            UIDebug.Set(3, $"Pos Lerp Tick: {GameManager.StopwatchMilliSecondsAvg2.ToString("0.000")}ms");
        }
    }
1

There are 1 answers

2
Andrew Łukasik On

The most immediate yet blunt solution here is to control OnUpdate calls with Enabled property; when set to false it will disable OnUpdate from being called:

protected override void OnCreate ()
{
    this.Enabled = condition;
}