• 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
Question by smirlianos · Apr 25, 2013 at 05:00 PM · javascripttransformpositionparentchild

Problem with making child an object

Hello! I want an object to be a child of an other when 24 seconds pass. Heres the script. It seems fine to me, but it doesn't work. Any help?

 #pragma strict
 
 var secondpos : Transform;
 function Start () {
 yield WaitForSeconds(24);
 Destroy(GetComponent(SmoothLookAt));
 yield;
 transform.rotation = Quaternion.identity;
 transform.position = secondpos.transform.position;
 gameObject.transform.parent = secondpos.transform;
 }
 
 function Update () {
 }
Comment

People who like this

0 Show 3
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 KiraSensei · Apr 25, 2013 at 05:23 PM 0
Share

Have you check in the inspector that secondpos is valuated ? secondpos is already a transform, I don't think you need to do secondpos.transform on lines 9 and 10

avatar image GC1983 · Apr 25, 2013 at 05:37 PM 0
Share

What lines 9 and 10 are doing are making sure that the child object is directly lined up with the parent object.

Whats the 'yield' on line 7 for? That might be where youre going wrong.

A good form of practice debugging is adding in a Debug.Log(); and place either the variable or a string to verify if the line is being executed. Place it before and after where ever between the code and see if its working. If nothing responds in the Debug, then you know where your problem starts.

avatar image KiraSensei · Apr 25, 2013 at 05:54 PM 0
Share

What i meant was that secondpos is already a transform, secondpos.transform is useless.

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by fafase · Apr 25, 2013 at 06:04 PM

New Answer based on new comments.

You say the camera moves with an animation to the empty game object and attach itself to this. Let's see if we can work this out.

 var secondPos:Transform;
 function Start(){
    StartCoroutine(CamAnim());
 }
 
 function CamAnim(){
    animation.Play("CamAnimation");
    while(animation.IsPlaying("CamAnimation")){
      yield;
    }
    transform.parent = secondPos;
 }

Since you have the animation done, it probably include already all movement and rotation so your camera should end up pretty much ready to go.

If not then you need to add some little details before and after the parenting, like you did in your example.

Comment

People who like this

0 Show 9 · 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 smirlianos · Apr 25, 2013 at 06:21 PM 0
Share

It doesn't work, the console says this:

MissingReferenceException: The variable secondpos of 'Js_CameraChange' doesn't exist anymore. You probably need to reassign the secondpos variable of the 'Js_CameraChange' script in the inspector. Js_CameraChange.ToBeDoneLater () (at Assets/Js_CameraChange.js:10)

avatar image KiraSensei · Apr 25, 2013 at 06:23 PM 0
Share

Did you check in the inspector that secondpos is valuated beore launching the game ?

avatar image smirlianos · Apr 25, 2013 at 06:29 PM 0
Share

Yes, it is

avatar image fafase · Apr 25, 2013 at 06:30 PM 0
Share

Did you declare the variable somewhere? You should know better than me what is secondpos and where it is.

avatar image KiraSensei · Apr 25, 2013 at 06:31 PM 0
Share

Did you try to debug it ? Put a breakpoint before using secondpos and have a look at its value.

Show more comments
avatar image

Answer by Vonni · Apr 25, 2013 at 05:48 PM

Remove yield, and remove all the unneccesary transform calls

 #pragma strict
  
 var secondpos : Transform;
 function Start () {
 yield WaitForSeconds(24.0);
 Destroy(GetComponent(SmoothLookAt));
 transform.rotation = Quaternion.identity;
 
 transform.parent = secondpos;
 transform.localPosition = Vector3.zero;
 }

But the object you have this script on is probably a character controller / rigidbody. (Am I right?). If so, linking wont do much. It only helps the rigid object inherit movement of parent if its sleeping I think.

Comment
Fred_Vicentin

People who like this

1 Show 2 · 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 smirlianos · Apr 25, 2013 at 06:04 PM 0
Share

Actually it's a camera. the project is a presentation. The script doesn't work, it moves he camera to 0,0,0 world position without being a child to he othe object.

Does it matter that the "secondpod" is already a child to a third object?

avatar image KiraSensei · Apr 25, 2013 at 06:32 PM 0
Share

It should not matter.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 People are following this question.

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

Related Questions

Camera follow ball along cylinder 1 Answer

Position of parent-Object 2 Answers

Moving parent to position of child 0 Answers

how to stop child object from twitching? 1 Answer

Transform child object over time and then destroy it. 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