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

Resolved RefRW<LocalTransform> : Local Transform Component missing from Entity

Discussion in 'Entity Component System' started by UNP0XBL, Jun 7, 2023.

  1. UNP0XBL

    UNP0XBL

    Joined:
    Feb 12, 2016
    Posts:
    7
    Diving into entities by following along with a tutorial and making updates where required.

    ArgumentException: A component with type:Unity.Transforms.LocalTransform has not been added to the entity.


    I get the above exception when trying to reference the LocalTransform component to my aspect. This LocalTransform should be on every entity right?

    Code (CSharp):
    1.     public readonly partial struct SpawnerAspect : IAspect
    2.     {
    3.         public readonly Entity Entity;
    4.  
    5.         private readonly RefRO<SpawnerProperties> _properties;
    6.  
    7.         private readonly RefRW<LocalTransform> _transform;
    8.         private readonly RefRW<RandomComponent> _random;
    9.  
    10.         public int EnemiesToSpawn => _properties.ValueRO.EnemiesToSpawn;
    11.         public Entity EnemyPrefab => _properties.ValueRO.EnemyPrefab;
    12.  
    13.     }
    Some more investigating shows that on Play, the entity is not getting a Local Transform Component

     
    Last edited: Jun 7, 2023
  2. UNP0XBL

    UNP0XBL

    Joined:
    Feb 12, 2016
    Posts:
    7
    To add, documentation on transforms has this paragraph

    The built-in transform system adds a LocalTransform component to each entity by default. It stores the data that represents an entity's position, rotation, and scale. There are also a variety of static helper methods defined on it.
     
  3. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    274
    ZGMT, Ayr and UNP0XBL like this.
  4. UNP0XBL

    UNP0XBL

    Joined:
    Feb 12, 2016
    Posts:
    7
    Solved! Thank you! The TransformUsageFlags I wasn't too knowledgeable on and had them in my Bake as None but this helped me understand a bit about how things are getting baked. My Bake code now looks like this,


    Code (CSharp):
    1.     public class SpawnerBaker : Baker<SpawnerMono>
    2.     {
    3.         public override void Bake(SpawnerMono authoring)
    4.         {
    5.             Entity entity = GetEntity(TransformUsageFlags.Dynamic);
    6.             AddComponent(entity, new SpawnerProperties
    7.             {
    8.                 FieldDimensions = authoring.FieldDimensions,
    9.                 EnemiesToSpawn = authoring.EnemiesToSpawn,
    10.                 EnemyPrefab = GetEntity(authoring.EnemyPrefab, TransformUsageFlags.None)
    11.             });
    12.  
    13.             AddComponent(entity, new RandomComponent
    14.             {
    15.                 RandomGenerator = Unity.Mathematics.Random.CreateFromIndex(authoring.RandomSeed)
    16.             });
    17.         }
    18.     }
    This line was changed from
    Entity entity = GetEntity(TransformUsageFlags.None);

    to:
    Entity entity = GetEntity(TransformUsageFlags.Dynamic);
     
    Ayr and macoson like this.