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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Bug Unity crashes after ~950 entities parented to previous entity

Discussion in 'Entity Component System' started by Codalyn, Mar 13, 2022.

  1. Codalyn

    Codalyn

    Joined:
    Dec 30, 2014
    Posts:
    41
    There appears to be a bug with parenting entities using the TransformSystem: when I create M entities, where entity N is parented to the entity N-1, Unity crashes after about 950 entities created.
     
  2. Fribur

    Fribur

    Joined:
    Jan 5, 2019
    Posts:
    127
    Do you mean you create a parent-child hierarchy that is 950 deep, or 950 children on a parent? The latter works without issues for me.
    upload_2022-3-13_10-44-59.png
     
  3. Codalyn

    Codalyn

    Joined:
    Dec 30, 2014
    Posts:
    41
    The parent-child hierarchy is 950 deep in my case.
     
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,635
    Which is ridiculous by the way. If they're not static and you have a bunch of these the cost to updating the local to world transforms for the deep children is going to blow out. Hierarchy optimization is about trying to keep it as flat as possible!
     
    Last edited: Mar 13, 2022
  5. Fribur

    Fribur

    Joined:
    Jan 5, 2019
    Posts:
    127
    Not quite sure (long time since I checked) but the Transform system might be implemented in a recursive way instead of iterative, in which case probably the stack explodes after 950 recursive function calls.

    Edit: LocalToParentSystem.cs, recursive call of ChildLocalToWorld:


    Code (CSharp):
    1.  void ChildLocalToWorld(float4x4 parentLocalToWorld, Entity entity, bool updateChildrenTransform)
    2. {
    3.      [...]
    4.      if (ChildFromEntity.HasComponent(entity))
    5.      {
    6.           var children = ChildFromEntity[entity];
    7.           for (int i = 0; i < children.Length; i++)
    8.           {
    9.                ChildLocalToWorld(localToWorldMatrix, children[i].Value, updateChildrenTransform);
    10.           }
    11.       }
    12. }
    13.  
     
    Last edited: Mar 13, 2022
    Anthiese, MNNoxMortem and Luxxuor like this.
  6. Codalyn

    Codalyn

    Joined:
    Dec 30, 2014
    Posts:
    41
    Nothing in my game is static. The depth of the tree will depend on what my players create. I expect the general case will be (far) below such a ridiculous depth... but I can't have the game crashing when a player comes up with something wild!

    Lol. That most certainly explains!

    I'm going to bypass Unity's TransformSystem for now, since the only thing I really need is the transforms of children responding to changes in their parent(s), which I've now achieved with a custom system.