Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Feb 27, 2015 at 02:40 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Exploder1010 · Feb 27, 2015 at 02:15 AM · parentchildparentingparent-child

Does transform.root work when a parent is made the child of another object through script?

Does transform.root work when a parent is made the child of another object through script? It isnt working for me. In one script I create a weapon, "item" with a child and a child of that child, which has a script that is used to detect collisions:

     Transform item =(Transform)Instantiate(itemPrefab, itemPosition.transform.position, itemPosition.transform.rotation);
     
     item.parent = itemPosition.transform;

On the child of the child of "item", the script which is used to detect collisions has this code:

         Transform playerTransform;
 void Awake () {
     playerTransform = transform.root;
 }

But transform.root returns only "item," not the parent of the parent of the parent... of item, which I though would be the root, and which I am trying to access. Doing a search for the tag of the very first parent won't work because I will have multiple players that all have this sort of item.

Is there any way to access the very first parent? I've also tried "player transform = transform.parent.transfrom.parent.transform.parent etc...", but that returned a null reference exception when I started accessing parents of "item."

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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by meat5000 · Feb 27, 2015 at 02:12 AM

root always refers to the top-most transform.

The problem is that you are making a 'snapshot' of a transform and probably assuming it will always refer to transform.root. It will simply take the instantaneous values of whatever the transform root is at that time.

Comment
Add comment · 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 Exploder1010 · Feb 27, 2015 at 02:35 AM 0
Share

I think you are right. It seems that the problem is that I used transform.root in the awake function, which was called before "item" was even parented to the other object.

avatar image meat5000 ♦ · Feb 27, 2015 at 02:37 AM 1
Share

Indeed, if you have moved your object hierarchy around, transforms assigned using an alias (parent, child, root) will need to be reassigned as they may very well refer to different transforms.

Using Start ins$$anonymous$$d of Awake usually has better results; as you notice, it takes time for the objects etc to load.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Getting the topmost parent 1 Answer

Make a simple tree 1 Answer

Adressing a Child Object after it´s unparented 0 Answers

How To Get List of Child Game Objects 14 Answers

Why is the order of parent and child different depending on where you select them? 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