- Home /
Temporary Parenting through Scripting
I have a script thats suppose to temporarily parent my object (player) to the animating elevator so it wont fall through the elevator as it goes up. then when its finished i want it to unparent it. however when i test it, it says that the variable "player" hasnt been assigned, even though i drag and dropped it into the player variable. heres my script:
var elevator : AnimationClip; //animation var player : GameObject; //object being parented to HoistCar
function OnTriggerEnter (myTrigger : Collider) {
if(myTrigger.gameObject.name == "player") { Debug.Log("elevator trigger activated"); //testing to see if trigger works HoistCar = GameObject.Find("HoistCar"); //finding gameobject player HoistCar.animation.Play("elevator"); //if trigger is activated, play //animation player.transform.parent = transform; //parent player to hoistcar if trigger is activated
}
else {
transform.parent = null; //unparent object if animation isnt playing
}
}
Answer by BinaryCore · May 05, 2011 at 11:57 PM
Did you drag and drop a Prefab of the player into your script variable in the designer? If so, you still need to call Instantiate to create an instance of your player or it won't exist yet at runtime.
A prefab is not an instance.
Answer by demize2010 · May 06, 2011 at 01:33 AM
Hey man,
Consider using the "collision" info from trigger enter.
EG (not tested)
function OnTriggerEnter (other : Collider) {
other.transform.parent = transform; // You may need other.gameObject.transform.parent = transform;
}
You'll obviously need to drop that parenting onTriggerExit() or similar.
Thanks for the reply, but I'm trying to make it so when you get on the elevator, (when the animation starts from a trigger) i want the person to be parented to the elevator until the animation stops. this means if the player leaves the trigger, he will fall through the elevator since objects apparently don't stay on animating objects.
This will parent the transform of your character to the transform of the elevator which seems to be what you are after?
Your answer
![](https://koobas.hobune.stream/wayback/20220613132045im_/https://answers.unity.com/themes/thub/images/avi.jpg)