Parenting and Unparenting a GameObject that has a Rigidbody

Hey, everyone. I’ve got moving platforms in my side scrolling game. I parented my character GameObject to it so that it stays on when it moves up and down and left to right.

The moving platform has a box collider parented to it and set as a trigger. When I step onto the platform the character parents to the platform, but when I exit or get off of the platform it never unparents and the Debug.Log never shows up for OnTriggerExit.

I’ve tried changing the parenting from OnTriggerEnter and OnTriggerStay, but seeing as how parenting isn’t the issue it doesn’t do much. The problem is that for some reason it never detects the GameObject exiting the trigger. The character has a rigidbody component attached to it and when I remove it, it unparents and parents perfectly.

Am I missing a step here? How do I get the object to parent/unparent while still having the rigidbody component?

Here’s the javascript code down below:

#pragma strict
var player : GameObject;
var platform : GameObject;

function Update()
{

}

function OnTriggerEnter(other : Collider)
{
	//if the colliding gameobject is the player...
	if(other.gameObject == player)
	{
		player.transform.parent = platform.transform;
		Debug.Log("Parented");
	}
}

function OnTriggerExit(other : Collider)
{
	//if the colliding gameobject is the player...
	if(other.gameObject == player)
	{
		player.transform.parent = null;
		Debug.Log("Unparented");
	}
}

Collisions are disabled between an object and it’s parent, OnTriggerExit is never called because collisions are not checked anymore after you have parented player GameObject.

Try Transform.DetachChildren on the platform

http://docs.unity3d.com/Documentation/ScriptReference/Transform.DetachChildren.html