I tried following this Unity ECS Entity Manager Tutorial from Turbo Makes Games. I'm using Unity 2020.3.20f1.1086.
The Code works initially but I noticed, that Line 20 in SpawnEntitiesSystem.cs EntityManager.SetComponentData(newEntity, newPosition); stops working when I change or remove the namespace. After tinkering a bit with the code I can reproduce that the specific namespace is not important but the namespace has to be at least 18 characters long for the code to work as expected. If I change the namespace to 17 characters all entities are spawned at the same place. Other code after the faulty line 20 was executed but for readability I removed it from the code example. (Originally user input was also handled and in OnStartRunning a component was added to the new entitiy)
I only heard of similar problems in e.g. C++ when using pointer maths but I thought something like this shouldn't happen in C#. Does anyone have an idea what the core problem could be?
What I already tried without succes
- restart Unity
- delete Library, Obj and Logs folders
- write code from scratch in new project
- restart PC
SpawnEntitiesSystem.cs
using Unity.Entities; using Unity.Mathematics; using Unity.Transforms; namespace ALongNamespaceABCD { public class SpawnEntitiesSystem : SystemBase { private int2 _entitySpacing; protected override void OnStartRunning() { var entitySpawnData = GetSingleton<EntitySpawnData>(); var gridSize = entitySpawnData.SpawnGrid; _entitySpacing = entitySpawnData.EntitySpacing; for (int x = 0; x < gridSize.x; x++) { for (int y = 0; y < gridSize.y; y++) { var newEntity = EntityManager.Instantiate(entitySpawnData.EntityPrefab); var newPosition = new LocalToWorld { Value = CalculateTransform(x, y) }; EntityManager.SetComponentData(newEntity, newPosition); } } } private float4x4 CalculateTransform(int x, int y) { return float4x4.Translate(new float3 { x = x * _entitySpacing.x, y = 1f, z = y * _entitySpacing.y }); } protected override void OnUpdate() {} } } EntitySpawnData
using Unity.Entities; using Unity.Mathematics; [GenerateAuthoringComponent] public struct EntitySpawnData : IComponentData { public Entity EntityPrefab; public int2 SpawnGrid; public int2 EntitySpacing; } Edit: I tried around a bit more and found out that it seems that the correct position will be set correctly at first. But it will instantly be overwritten by the prefabs Transform component. So maybe it has to do with converting the Prefab to an entity (I use the default Convert to Entity component) or a problem with some internal multithreading occurs.
ALongNamespaceABCD