How do I center a GameObject on its children?

I can do this using the editor but I'm trying to accomplish same using the API. Couldn't find anything the script reference. Any help would be highly appreciated. Thx.

Sum the transform.position of all of the children, then, divide that by the count of the children. This is the centered position. Set each of the children's parent to null, then move the parent object to that position, and then set the children's parent to the parent object again.

You can try removing the parent of the child, set the position of the main object to its child, then reparenting

You could save the current combined position of the children in a var, then center the children to the parent (maybe using transform.localPosition or whatever suits your specific need), and then move the parent along with its children back to the saved previous position.

Since you'll be doing all this in one frame it'll be as realtime as it gets. I don't know how expensive this will actually be but it could be worth a shot. Hope this helps, good luck!

If you are trying to move the parent relative to the children, it sounds like you may have your parent/child relationship backwards or sideways. What benefits is the parent/child relationship getting you besides a headache trying to solve this? If nothing, just get rid of it.

If the two things were siblings or the parent/child relationship was swapped, for example, you could just set the center of one to the center of the other.