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

Bug SystemAPI.Query with Managed components in foreach doesn't work.

Discussion in 'Entity Component System' started by itsDmajster, Dec 14, 2022.

  1. itsDmajster

    itsDmajster

    Joined:
    Oct 27, 2014
    Posts:
    45
    I tried moving my camera around in the game with ECS but found out that there is no runtime representation for it, so I tried just handling the managed component, but no matter what I tried the foreach never ran. Upon trying to figure out what the cause was I found out that also the example with the Transform doesn't work. I tried placing the entity with the camera both in the subscene and the scene in neither case did I get any debug logs that anything ran.

    Unity version 2022.2.0f1, Entities 1.0.0-pre.15

    Code (CSharp):
    1. using Unity.Entities;
    2. using UnityEngine;
    3.  
    4. public partial class PlayerCameraSystem : SystemBase
    5. {
    6.     protected override void OnUpdate()
    7.     {
    8.         foreach (var transformRef in SystemAPI.Query<SystemAPI.ManagedAPI.UnityEngineComponent<UnityEngine.Transform>>())
    9.         {
    10.             Debug.Log("test");
    11.             transformRef.Value.Translate(0, 1, 0);
    12.         }
    13.     }
    14. }
    15.  
     
  2. DaxodeUnity

    DaxodeUnity

    Unity Technologies

    Joined:
    Aug 5, 2021
    Posts:
    27
    Hi :)

    How did you set up the Entity? Your example should query all entities with the managed component 'Transform'.

    As an example. Say i had a System:
    Code (CSharp):
    1. partial struct MySystem : ISystem {
    2.     public void OnCreate(ref SystemState state){
    3.         var entity = state.EntityManager.CreateEntity();
    4.         state.EntityManager.SetName(entity, "MyEntity"); // note this is just a debug name, and not actually useful in your built games.
    5.         var myTestObject = new GameObject("MyCodeMadeGO"); //statics and other ways to find an actual GO works too
    6.         state.EntityManager.AddComponentObject(entity, myTestObject.transform);
    7.     }
    8.     public void OnUpdate(...){}
    9.     public void OnDestroy(...){}
    10. }
    Now the query you made would find the transform of "MyCodeMade GO ..." as an entity exists with that transform. Note I used ISystem as an example but SystemBase works just as well in your case :)