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

Question Attempted to access ComponentLookup which has been invalidated by a structural change.

Discussion in 'Entity Component System' started by frankfringe, Feb 19, 2023.

  1. frankfringe

    frankfringe

    Joined:
    Feb 9, 2019
    Posts:
    83
    Hello the following error is thrown
    Code (CSharp):
    1. ObjectDisposedException: Attempted to access ComponentLookup<Unity.Collections.NativeText.ReadOnly> which has been invalidated by a structural change.

    for the following code:
    Code (CSharp):
    1. using Unity.Burst;
    2. using Unity.Collections;
    3. using Unity.Entities;
    4. using Unity.Mathematics;
    5. using Unity.Rendering;
    6. using Unity.Transforms;
    7. using UnityEngine;
    8. using Random = Unity.Mathematics.Random;
    9.  
    10. [BurstCompile]
    11. partial struct MonsterSpawningSystem : ISystem
    12. {
    13.     [ReadOnly] ComponentLookup<WorldTransform> WorldTransformLookup;
    14.     public Entity player;
    15.     public float timeUntilNextMonster;
    16.     public EntityQuery monsterSpawnerQuery;
    17.     public Random random;
    18.  
    19.     [BurstCompile]
    20.     public void OnCreate(ref SystemState state)
    21.     {
    22.         WorldTransformLookup = state.GetComponentLookup<WorldTransform>(true);
    23.         monsterSpawnerQuery = SystemAPI.QueryBuilder().WithAll<MonsterSpawner>().WithAspect<TimerAspect>().Build();
    24.         random = new Random(8328732);
    25.     }
    26.  
    27.     [BurstCompile]
    28.     public void OnDestroy(ref SystemState state)
    29.     {
    30.      
    31.     }
    32.  
    33.     //[BurstCompile]
    34.     public void OnUpdate(ref SystemState state)
    35.     {
    36.         var dt = SystemAPI.Time.DeltaTime;
    37.         //var config = SystemAPI.GetSingleton<Config>();
    38.  
    39.         var ecbSingleton = SystemAPI.GetSingleton<BeginSimulationEntityCommandBufferSystem.Singleton>();
    40.         var ecb = ecbSingleton.CreateCommandBuffer(state.WorldUnmanaged);
    41.         foreach (var (monsterSpawner, timerAspect) in SystemAPI.Query<MonsterSpawner, TimerAspect>())
    42.         {
    43.             if (timerAspect.isNextNow(ref random, dt))
    44.             {
    45.                 Debug.Log(monsterSpawner.SpawnCenter);
    46.                 var instance = ecb.Instantiate(monsterSpawner.MonsterPrefab);
    47.                 var positionNoise = new float3(random.NextFloat(), 0, random.NextFloat()) * 5;
    48.                 var spawnAt = WorldTransformLookup[monsterSpawner.SpawnCenter];
    49.                 var spawnAtPosition = positionNoise;
    50.                 //Debug.Log(spawnAtPosition);
    51.                 var transform = LocalTransform.FromPosition(spawnAtPosition);
    52.                 Debug.Log(transform);
    53.                 ecb.SetComponent(instance, transform);
    54.             }
    55.         }
    56.     }
    57. }

    Any pointers on how to debug this would help me a lot!
    Thanks
     
  2. frankfringe

    frankfringe

    Joined:
    Feb 9, 2019
    Posts:
    83
    Thanks to the very helpful discord (@tertle ), I could resolve this and what one needs to do is call
    Code (CSharp):
    1. WorldTransformLookup.Update(ref state);
    every frame
     
    OrientedPain and Mikaelan like this.