I need to enforce the order in which multiple children are added to a parent object in a single method call. Help?

I can’t be too specific about what I’m doing, so I can’t put any code and if you need to see the code, I apologize and you might want to turn back now. This isn’t a question about code as much as it is about how the back end of Unity handles parenting (using C#).

Basically, there is a type of object in our game that I am formulating as a binary tree. If you add two nodes together, they form a simple tree of two nodes. You can add two nodes together to get a tree. You can add a node to a tree and get a tree. You CANNOT add two trees together and get a tree. They will not connect. Pieces connect when they are a match and their colliders align.

The problem I am running into is that when Unity loaded up saved pieces, they were saved as individual nodes with their positions, so Unity loads them up and if they are eligible to connect to form trees, they automatically snap to do so. The problem is there is no order in which this snapping occurs. One could start at a leaf node and the other could start at the root. When they reach a point where they should all be connected, they can’t, because they’re separate trees.

I reformulated the way they were saved into a tree-like hierarchy and load each “tree” root first. If it has a left child, recursively load that node. If it has a right child, recursively load that one (you’ll notice this is essentially a depth-first traversal). In the base case, you have just one node. In the base+1 case, you have the root node adding its left child, forming a tree. Then in the n+1 step you have a single node being added onto the tree.

The problem I’m running into is, even though I create the pieces, then set their position etc. and then set their parent to the game board in an explicit order, nothing is added until the method exits, meaning that despite how I wrote the method, they are being added all at the same time; essentially how it was before.

Is there a way to “flush” what’s been added after I “add” each one programmatically so they show up in the game field? Is there a method by which I can make the parent object acknowledge its new children when I need it to rather than when the method exits? Would multi threading the method that adds the pieces help?

I noticed that the problem was that they’re still being added in an arbitrary order despite me attempting to enforce an order when I tried to put a wait call between the placement of the piece and the recursive calls. I noticed the sum total of the wait time occurred, but not when it should; it only occurred all at once, then all the pieces were added at the same time. I verified this by stepping through with the debugger, too.

Pseudocode:

void Make_Trees (){

    List<XmlNode> roots = get_roots( XmlDoc );

    foreach ( XmlNode node in roots ){

        Create_Tree( node );
    }

}

void Create_Tree( XmlNode root){

    Transform the_parent = GameObject.Find("Parent").transform;
    Transform new_item = parse_xml_into_piece(root);

    new_item.parent = the_parent; //This is where I want it to be added.

    /*
     * Is there a thing I can add here, like, "parent.update"
     * or flush the stack or something to force it to show
     * the new children?
     */

    if ( has_left_child( root ) ){
        Create_Tree ( get_left_child(root ) );
    }

    if ( has_right_child( root ) ){
        Create_Tree( get_right_child(root ) );
    }
}

In this case, the pieces would not be added until the “Make_Trees” method terminates, but I’d want them to be added individually each time I set an object’s parent to the “Parent” object.

Thank you VERY much for taking the time to read this, and I really hope someone can help me out.

TL;DR: I need children objects added to a parent object in a very specific order and I do it in a recursive method and regardless of the order I use, nothing is added until the method terminates at which point all of the items are added at once. How can I make the object acknowledge the added children mid-method?

I’m not sure what is the issue with your description (a toy example reproducing your problem will be really helpful). But I can give you a couple of ideas:

  • You can use Awake method to trigger things on create (but from your code looks like the objects are already created and you are only parenting).
  • You can define scripts execution order: select the script in the Project view and click the Execution Order button in the Inspector.
  • You can use BroadcastMessage to tell the objects in what order they should parent.

This sounds like a typical representation vs, object issue. If I’m getting it: you change the gameObject tree structure, then immediately examine it as data.

The way I solve those is to only change/examine the representation. Like arranging cut-outs of furniture on graph paper. Then, afterwards, write it out to the real object.

I assume you have an internal tree data-structure, with links to all the the actual Transforms. You could just rearrange that. Then, afterwards, traverse it and “write out” everyone’s new parent, to the actual Transforms.