- Home /
When collider enters trigger, colliders parent = triggers parent
I'm trying to write some code so that when a gameObject enters a Trigger, it parents that gameObject to the triggers own parent. However, I'm having some difficulty accessing the Transform of said object in my code. Here's some examples of what I've tried:
function OnTriggerEnter (other : Collider){
if (other.transform.tag == "Moveable")
{
other.transform.parent = transform.parent;
}
}
And:
var otherTrans; function OnTriggerEnter (other : Collider){
if (other.transform.tag == "Moveable") { otherTrans = other.transform;
otherTrans.parent = transform.parent;
} }
I've tried a few other things too, but they're all variations on this. I've tried looking through the script references for inherited members, but I still can't find a way. What is the exact set of inherited members I need, or does my code need changed?
Thanks
P.S. all the colliders which it would pick up are rigidbodies.
Would otherTrans.parent.gameObject = transform.parent.gameObject be what you are looking for?
Answer by Eric5h5 · Apr 05, 2011 at 07:26 PM
Your first code example will work as-is, although you don't need to get the tag from the transform component (it's inherited from GameObject anyway), and using CompareTag is better.
if (other.CompareTag("Moveable")) {
Put Debug.Log statements in to see if the trigger event is happening, and what the other's tag actually is.
It's not picking up the colliders entering the trigger. I seem to have a terrible record with triggers, they never seem to work for me. Evidently I'm doing something wrong with them.
I thought it was as simple as if a collider enters the trigger, it sends the message that starts the function?
@Alienjesus: Have you looked at the chart here? http://unity3d.com/support/documentation/Components/class-$$anonymous$$eshCollider.html Not everything generates trigger messages.
But in this case, a static trigger is colliding with a rigidbody collider, so it should.
Changed the trigger to a kinematic rigidbody trigger, everything works fine now.