Child of parent issue.
I have 4 spheres which land on top of each other and when there is a collision the sphere will become a child of the one it hits but for some reason after all collisions the hierarchy looks like this:
>red
>red
>red
>red
My intentions are to have it look like this (one parent and 3 child objects):
>red
red
red
red
How would I go about achieving this?
var whatColor : String;
function OnCollisionEnter (other : Collision)
{
if(whatColor == "red" other.gameObject.tag == "red")
{
other.transform.parent = transform;//makes it a child
}
if(whatColor == "yellow" other.gameObject.tag == "yellow")
{
other.transform.parent = transform;
}
rigidbody.isKinematic = true;
}
Any help would be greatly appreciated! Im stumped?
You can check if the gameobject has a child. And keep checking if that child has a child until it doesn’t, then that gameobject will be the parent.
Sorry if that sounds confusing XD
haha, yes that does sound confusing… you lost me there.
Would you have a simple example so I can see what you mean?
My intentions are that once there are 3 child objects then destroy the parent something like this:
function Update()
{
if(transform.childCount > 2)
{
Destroy(gameObject);
}
}
But it wont work until I get the hierarchy correct.
Lol, something like this:
Transform parent = transform;
while(parent.childrenCount > 0)
{
foreach(Transform child in parent)
{
parent = child;
}
}
what this would do is make the “parent” transform by default then while that transform has children it will make that transform equal to the child. So essentially it will keep making children the “parent” until there are no children.
I typed this up int he reply to thread so hopefully there are no syntax errors.
EDIT:
sorry I made a few mistakes
put “transform” when I meant “child”
Greatly Appreciated!!!
No worries if there are syntax errors, i’ll try and sort it
I think I see the process thank you very much! and if you celebrate HAPPY HOLIDAYS to you and your family. Again thank you very much 
No problem 
Happy Holidays to you too.
I really appreciate the help Straight Rainbow and hate to bother you again.
I converted the code to javascript and believe Im almost there? For some reason Im getting half results. I added more spheres to the mix and now half of them will go into the hierarchy right and the others still go the other way? Maybe its the way I tried to implement your suggestions.
var whatColor : String;
function OnCollisionEnter (other : Collision)
{
if(whatColor == "red" other.gameObject.tag == "red")
{
other.transform.parent = transform;
var parent : Transform = transform;
while(parent.childCount > 0)
{
for(var child : Transform in parent)
{
parent = child;
}
}
}
rigidbody.isKinematic = true;
}
No worries.
I think it would be this.
Because your is just parenting it to the one it collides with, not the child.
var whatColor : String;
function OnCollisionEnter (other : Collision)
{
if(whatColor == "red" other.gameObject.tag == "red")
{
var parent : Transform = transform;
while(parent.childCount > 0)
{
for(var child : Transform in parent)
{
parent = child;
}
}
other.transform.parent = parent;
}
rigidbody.isKinematic = true;
}
I think that code would work.