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

Problem Instantiate GO in a hybrid Solution

Discussion in 'Entity Component System' started by daserra, Sep 1, 2018.

  1. daserra

    daserra

    Joined:
    Dec 7, 2016
    Posts:
    14
    Hi,

    I'm trying to Instantiate a GO ENTITY but im getting
    "InvalidOperationException: The NativeArray has been deallocated, it is not allowed to access it"

    So here is the situation, I have the HitSystem, and i attatched a Monobehavior script to entities to have easy access to some prefabs.

    here is my struct
    Code (csharp):
    1.  
    2. public struct Data
    3. {
    4. readonly public int Length;
    5. public EntityArray Entity;
    6. public ComponentDataArray<Hit> Hit;
    7. public ComponentArray<ObjectCollider> ObjectCollider; // MonoBehavior Script
    8. }
    9.  
    And here is how im trying to Instantiate a prefab which IS AN ENTITY, that is basically a particle effect
    Code (csharp):
    1.  
    2. for(int i = 0; i< data.Length; i++)
    3. {
    4. var entity = data.Entity[i];
    5. var hit = data.Hit[i];
    6. var objectCollider = data.ObjectCollider[i];
    7.  
    8. var particle = Object.Instantiate(objectCollider.particleOnCollision);
    9. Object.Destroy(particle, 0.5f);
    10. PostUpdateCommands.RemoveComponent<Hit>(entity);
    11. }
    12.  
    13.  
    Note that if I try to Instantiate a NON-ENTITY GO it works without Error.

    I'm missing something, but I thought the new object has nothing to do with the current object.
     
  2. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    157
    Do the objects you are instantiating or destroying have a GameObjectEntity component? If so, you can't do that while looping over your component data. That component ends up creating or destroying an entity and its related components, which invalidates the arrays you are looping over.

    One solution to this is to create a command buffer (just a list with the data you need to instantiate or destroy those objects). After you loop over you components and collect your data, then loop over the command buffer and instantiate /destroy your objects.
     
    daserra likes this.
  3. daserra

    daserra

    Joined:
    Dec 7, 2016
    Posts:
    14
    Thanks for the answer.

    I get it, why the error happens, i just dont get your solution, I’ve used commands buffer for create or setting entities data. I dont know how to create a command buffer only with a list and then interact with that later.

    But you give me an idea how to deal with that situation.

    Code (csharp):
    1.  
    2. List<GameObject> toInstantiate;
    3.  
    4. Loop entity array
    5. {
    6.  toInstantiate.add(someGo);
    7. }
    8.  
    9. Loop toInstantiateList
    10. {
    11.  Instantiate;
    12. }
    13.  
     
  4. dstilwagen

    dstilwagen

    Joined:
    Mar 14, 2018
    Posts:
    36
    I answered a similar question last week you can find it here as a simple example. It is similar to the method from your pseudo code. Later in the thread I also posted an example of how to use a second world and system for creating GameObjects. It can be found here example of using a second world.
     
  5. daserra

    daserra

    Joined:
    Dec 7, 2016
    Posts:
    14
    Awsome bro, thanks I will check it out