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

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

Discussion in 'Scripting' started by HitmanDrummer, Jul 16, 2015.

  1. HitmanDrummer

    HitmanDrummer

    Joined:
    Jul 16, 2015
    Posts:
    2
    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!
    Code (CSharp):
    1. int childMax = 1;
    2.  
    3.     void Update()
    4.     {
    5.         childMax = transform.childCount;
    6.  
    7.         if(childMax > 1)
    8.             {
    9.                 transform.DetachChildren();
    10.             }
    11.     }
    12. ----------------------------------------------------------------------------
    13.     int childMax = 1;
    14.  
    15.     void Update()
    16.     {
    17.         childMax = transform.childCount;
    18.        
    19.         for (int chCnt = 1; chCnt > childMax; chCnt--)
    20.         {
    21.             transform.DetachChildren();
    22.         }
    23.  
    24.         Debug.Log (childMax);
    25.     }
    26. ----------------------------------------------------------------------------
    27. int childMax = 1;
    28.  
    29.     void Update()
    30.     {
    31.         childMax = transform.childCount;
    32.        
    33.           while(childMax > 1)
    34.         {
    35.             transform.DetachChildren();
    36.     break;
    37.         }
    38.  
    39.         Debug.Log (childMax);
    40.     }
     
  2. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    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.

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. public class LimitChildAmount : MonoBehaviour
    5. {
    6.     public int maxChildAmount = 1;
    7.  
    8.     void Update()
    9.     {
    10.         if(transform.childCount > maxChildAmount)
    11.         {
    12.             for(int i = maxChildAmount; i < transform.childCount; i++)
    13.             {
    14.                 transform.GetChild(i).SetParent(null);
    15.             }
    16.         }
    17.     }
    18. }
    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))"
     
    OmerPasa3328 likes this.
  3. EliteWalrus

    EliteWalrus

    Joined:
    Aug 16, 2014
    Posts:
    44
    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.
    Code (CSharp):
    1. void Update() {
    2.         Transform[] children = new Transform[transform.childCount];
    3.  
    4.         for (int c = 0; c < children.Length; c++) {
    5.             children [c] = transform.GetChild (c);
    6.         }
    7.  
    8.         int iteration = 0;
    9.         while (transform.childCount > 1) {
    10.             children [iteration].SetParent (null);
    11.             iteration ++;
    12.         }
    13.     }
     
    OmerPasa3328 likes this.
  4. HitmanDrummer

    HitmanDrummer

    Joined:
    Jul 16, 2015
    Posts:
    2
    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, http://docs.unity3d.com/ScriptReference/Transform-parent.html, 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.

    Code (CSharp):
    1. Collider theChild;
    2. GameObject myParent;
    3.  
    4. void DoThis()
    5. {
    6.      if(myParent.transform.childCount > 1)
    7.         {
    8.             theChild.transform.parent = null;
    9.         }
    10. }
     
    HiddenMonk likes this.
  5. bear1998

    bear1998

    Joined:
    Oct 1, 2018
    Posts:
    2
    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
     
  6. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637

    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.
     
    bear1998 likes this.