parent property

Component? get parent

Who owns this component in the component tree.

This can be null if the component hasn't been added to the component tree yet, or if it is the root of component tree.

Setting this property to null is equivalent to removeFromParent. Setting it to a new parent component is equivalent to calling addToParent and will properly remove this component from its current parent, if any.

Note that the parent setter, like add and similar methods, merely enqueues the move from one parent to another. For example:

coin.parent = inventory;
// The inventory.children set does not include coin yet.
await game.lifecycleEventsProcessed;
// The inventory.children set now includes coin.

Implementation

Component? get parent => _parent;
set parent (Component? newParent)

Implementation

set parent(Component? newParent) {
  if (newParent == null) {
    removeFromParent();
  } else {
    addToParent(newParent);
  }
}