Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Clarinet · Jul 19, 2012 at 02:35 PM · parentchild

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Wiki

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.

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Clarinet · Jul 19, 2012 at 03:25 PM 0
Share

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

avatar image Mander · Jul 19, 2012 at 03:58 PM 0
Share

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

avatar image Clarinet · Jul 19, 2012 at 05:17 PM 0
Share

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.

avatar image Mander · Jul 19, 2012 at 06:26 PM 0
Share

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

avatar image Clarinet · Jul 19, 2012 at 06:56 PM 0
Share

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?

Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to destroy a parent object when child object is collided 1 Answer

stop animations when an other animation is playing 2 Answers

"Center On Children" programmatically 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges