- Home /
Child of Parent
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;
}
My intentions in the end are that once there are 3 child objects then destroy the parent something like this:
function Update()
{
if(transform.childCount > 2)
{
Destroy(gameObject);
}
}
But I can't see if it will work until I get the hierarchy correct. Can someone give me an example of how to code this properly?
Answer by loihb · Dec 28, 2012 at 04:09 AM
other.transform.parent = transform.root; ?
Yes thats it! (transform.root) How do I mark this as answered? I did not realize there was a .root that can be used :)
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Transform child object over time and then destroy it. 1 Answer