- Home /
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!
Answer by fafase · Jul 19, 2012 at 02:58 PM
It is called parenting. It is all there.
http://docs.unity3d.com/Documentation/ScriptReference/Transform-parent.html
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.
Okay, I should have been more thorough in my question. This is good for parenting the object being triggered. I'll change my question.
this is how its done. just find the other gameObject that u want to interact with
What do you mean "find the other gameObject"? The script up there (with a few adjustments) works; it makes a gameobject a parent of the object with the script. This is not exactly the question, which is making a gameobject a parent of an object OTHER than the one with the script.
function Start(){ //find the objects u r going to parent parentObject= gameObject.Find("object3"); parentObject2 = gameObject.Find("object2"); } //trigger the script function OnTriggerEnter (hit : Collider){ parentObject2.transform.parent=parentObject.transform; //parent gameobjects yield WaitForSeconds(10.0); // wait for 10 secs parentObject2.transform.parent=null;// unparent }
i hope this helps
Okay I like your answer; it does work. But from your comment, I'm not sure where the WaitForSeconds goes in the script. Would you $$anonymous$$d explaining?
Answer by Mander · Jul 19, 2012 at 06:15 PM
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.