x


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!

more ▼

asked Jul 19 '12 at 02:35 PM

Clarinet gravatar image

Clarinet
11 8 14 18

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Jul 19 '12 at 06:15 PM

Mander gravatar image

Mander
473 2 5

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jul 19 '12 at 02:58 PM

fafase gravatar image

fafase
10.5k 9 15 42

Okay, I should have been more thorough in my question. This is good for parenting the object being triggered. I'll change my question.

Jul 19 '12 at 03:25 PM Clarinet

this is how its done. just find the other gameObject that u want to interact with

Jul 19 '12 at 03:58 PM Mander

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.

Jul 19 '12 at 05:17 PM Clarinet

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

Jul 19 '12 at 06:26 PM Mander

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 mind explaining?

Jul 19 '12 at 06:56 PM Clarinet
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5093
x426
x414

asked: Jul 19 '12 at 02:35 PM

Seen: 361 times

Last Updated: Jul 20 '12 at 06:35 PM