Set a parents child to allow only one child per object.

Hello everyone, I’m new to the community and I seem to be stuck on the title here. My goal is to limit an object to having only one child. I have an object in game and two children attached for the sake of testing but it doesn’t seem to remove the second child in any code. I have tried the following scripts, in various different methods, but I can’t seem to get it right. I would greatly appreciate some insight to my discrepancy!

int childMax = 1;

    void Update()
    {
        childMax = transform.childCount;

        if(childMax > 1)
            {
                transform.DetachChildren();
            }
    }
----------------------------------------------------------------------------
    int childMax = 1;

    void Update()
    {
        childMax = transform.childCount;
       
        for (int chCnt = 1; chCnt > childMax; chCnt--)
        {
            transform.DetachChildren();
        }

        Debug.Log (childMax);
    }
----------------------------------------------------------------------------
int childMax = 1;

    void Update()
    {
        childMax = transform.childCount;
        
          while(childMax > 1)
        {
            transform.DetachChildren();
    break;
        }

        Debug.Log (childMax);
    }

This should work. Just put it on any object you want to limit the child amount. You can set the child limit in the inspector, or just leave it at the default of 1.

Click for code

using System;
using UnityEngine;

public class LimitChildAmount : MonoBehaviour
{
    public int maxChildAmount = 1;

    void Update()
    {
        if(transform.childCount > maxChildAmount)
        {
            for(int i = maxChildAmount; i < transform.childCount; i++)
            {
                transform.GetChild(i).SetParent(null);
            }
        }
    }
}

Not sure if you want to be doing this every update, maybe just do it at Start.
Also keep in mind that this does not delete the child objects, it only makes it so they have no parent. If you want to destroy the child objects, then you can probably just do “Destroy(transform.GetChild(i))”

1 Like

Hidden Monk’s is much better but since I already did it, here is another way it could be done.

This will remove all but one.

void Update() {
        Transform[] children = new Transform[transform.childCount];

        for (int c = 0; c < children.Length; c++) {
            children [c] = transform.GetChild (c);
        }

        int iteration = 0;
        while (transform.childCount > 1) {
            children [iteration].SetParent (null);
            iteration ++;
        }
    }
1 Like

Thank you both very much for your assistance! I’m pretty new to all of this and I’m very grateful for your help. Although, my question was pretty general, I had been stuck for a day on it. Yet both of these methods worked perfectly, they were flawless for start and initialization and it might have helped if I explained I have a while mouse down on the object to set as child; and on mouse up on parent to disown the child. As suggested, I believe it would be most efficient to move everything from the mouse input statements into a separate function and then just call that function during mouse input.
After a good bit of long deliberation here, Unity - Scripting API: Transform.parent, which is kind of a mind bender for a noob like myself. I found it was more efficient to use the code below on the object (to become child) itself. Mainly for maintenance to assure that only one object can be the child at any time during mouse functions. I’m sure there is more optimization that can be done but it works for now and when I have more experience I may inquire about doing so. Again, thank you so very much for your input! I feel it really helped me think logically and come to a sufficient working code.

Collider theChild;
GameObject myParent;

void DoThis()
{
     if(myParent.transform.childCount > 1)
        {
            theChild.transform.parent = null;
        }
}
1 Like

hi I know its been years since this post has been active but I am also stuck on this. I need to only allow one child per parent at any given time, I have tried all of these techniques but none of them seem to work for me. I have already scripted the part that allows me to parent when the button is held and un parent when released however I would like to restrict the amount of children the parent can have. once the box is parented when I walk to the other one that parents as well and results in two boxes rotating inside each other following my player object.

I would also like to add that the parenting script is on the object that is picked up (the child) and not the player object

Hi, @bear1998

“I have already scripted the part that allows me to parent when the button is held and un parent when released however I would like to restrict the amount of children the parent can have.”

I’m not sure what you are actually trying to do, but if you already have code to parent something, then just add to your script a way to keep count of current added children.

“I have tried all of these techniques but none of them seem to work for me.”

Show the code if you are stuck with some problem, but the logic is very simple.

Create some script with a method in your to-be parent object, like TryAddChild(Transform newChild), and make it only add new children if the child count is less than maxCount - 1.

1 Like