How to make a child & then undo through scripting

Hey there, I was wondering if there was a way to make an object a child of another object (other than the one being touched to set off the trigger), but then undoing the action after a given time set, through scripting. So I’m thinking it could look something like this (in Javascript):

function OnTriggerEnter (hit : Collider){
//make an object a child;
//make the object independent, TimeSet;
}

So basically, object1 is touched, and object2 becomes a child of object3. Then after a given number of seconds, the bond is broken.

Thanks for helping!

It is called parenting. It is all there.

var parentObject:Transform;
var childObject:Transform;
var enter:boolean;

function Start(){
parentObject= GameObject.Find("Papa").transform;}

function Update(){
   if(enter)
       Parenting();
}

function OnTriggerEnter (hit : Collider){
    enter = true;
}
function Parenting (){
     childObject.transform.parent=parentObject.transform;
     yield WaitForSeconds(5.0);
     childObject.transform.parent = null;
     enter = false;
}

EDIT: Now that I have a better on what you want I think this version will do better.
To use WaitForSeconds you need a coroutine. Collision functions are callback functions. Your problem is OnCollisionEnter is called when the engine detects a collision and only on the frame it occurs. It means the compiler goes through the codes, parent and finds wait but never comes back (hopefully that makes sense). You need a function related to the Update that is run every frame so that the compiler gets back to the function until it is done.

In the new script, enter is switched so that the update calls the coroutine Parenting, on the first frame, the child is parented, then the wait function starts, the next 5 seconds every time the update goes into the function it hits the wait that is decreased each time until comes the frame when the wait is done and the unparenting is launched.

I guess you understood all that.

parentObject= GameObject.Find("Papa").transform; transform.parent=parentObject.transform;

affects the gameObject where the script is attached

if u want to affect another gameObject. u have to do specify which one, just as u are affecting papa.

`
parentObject= GameObject.Find(“Papa”).transform;
parentObject2 = GameObject.Find(“hijo”);

function OnTriggerEnter (hit : Collider){
parentObject2.transform.parent=parentObject.transform;
}

`

so that way the script will know which gameObject to affect.