- Home /
Parent, child, bone problem
Odd issue here that I can't seem to fathom. I have an object that collides with an animated rope. The rope is made up of 10 sprites using bones to animate the rope swinging. I used a .PSB for the rope and PSD importer to import the PSB to add the skeleton. Each bone has a collider2d and when the player object collides with the rope I want to delete all the colliders. On collision I get the gameObject (bone) connected to the collider2D I then use:
foreach (Transform child in transform.root)
Using debug to list the children I get the list of sprites (which have no colliders) that the bones are connected to but if i return the 'bone.transform.parent", I do indeed get the parent bone. Something happens when I goto the root parent of the bone which now iterates through the child sprites not the child bones.
I hope that makes sense, if anyone can shed some light on the problem
The Code:
public void OnTriggerEnter2D(Collider2D colInfo)
{
player = GameObject.FindWithTag("playerOne");
if (player.transform.parent == null)
{
player.GetComponent<Collider2D>().isTrigger = false;
Destroy(player.GetComponent<Rigidbody2D>());
player.transform.parent = bone.transform;
foreach (Transform child in transform.root)
{
Debug.Log(child.gameObject.name);
//child.gameObject.GetComponent<Collider2D>().enabled = false;
}
}
}
Answer by Daviekhan · Aug 08, 2019 at 10:31 AM
Found the answer, don't go to the transform.root work from the bone collided with:
Component[] colChild = GetComponentsInChildren(typeof(Collider2D), true); foreach (Collider2D colC in colChild) colC.enabled = false;
Component[] colParent = GetComponentsInParent(typeof(Collider2D), true); foreach (Collider2D colP in colParent) colP.enabled = false;
Your answer