Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to ComponentData to an entity at for loop?

Discussion in 'Entity Component System' started by optimise, Apr 6, 2018.

  1. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,114
    I have a ComponentSystem and I inject the entities I want to iterate. I want to add an ComponentData and do simple operation when iterating the entities inside for loop but it gives me "InvalidOperationException: The NativeArray has been deallocated, it is not allowed to access it" error. It seems like it's not allowed to add component to an entity while iterating inside for loop. Is that anyway way to do it?
     
  2. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    check PostUpdateCommands
     
  3. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,114
    Thanks. Problem solved.