Child an Object to Another Objects Parent On Collision
Hello all, I hope the title didn't confuse you, I'm sorry if it did.
Basically what I'm trying to do is have one Parent that is a platform be able to collect objects that land on it. And I need those objects to be able to stack up on each other. But I don't want the new object to child itself to the one it just hit, but rather to be childed to the other objects parent.
So there is one parent (the platform) and the rest of them stack up to be children of that one platform, not of each other. Is there any way you know of to do this?
Here is what I have, it's a script linked to the platform, it works, the objects that land on it get childed to the platform, but the ones that land on top of those objects, don't.
void OnCollisionEnter2D (Collision2D collision) {
if (collision.gameObject.tag == "Tag")
{
collision.transform.parent = transform;
}
}
Any help at all is greatly appreciated thank you!
Answer by ParadoxSolutions · Mar 18, 2016 at 12:25 AM
Untested, this is from memory but should work.
void OnCollisionEnter2D (Collision2D collision) {
//If the object that is colliding has the tag "Tag"
if (collision.gameObject.tag == "Tag")
{
//Set the parent of that object to the platform
collision.transform.parent = GameObject.Find("name of platform").transform;
}
}
You could also do GameObject.FindGameObjectWithTag("platform tag").transform
if you want.
by setting the parent to transform I think you were parenting the object to whatever it collided with.
Wow thank you so much, that worked perfectly.
Just for anyone else looking for this later, my code above goes on the platform. FirefightGI's goes on the objects.
Thanks again Fire, and thank you for the other method using the tags, that'll be good for future reference.
Edit; scratch that, you don't even need my code on the platform.