• 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 mdolnik · Oct 23, 2013 at 08:20 PM · gameobjecttransformparenthierarchy

Why is there no gameObject.parent?

Is there any reason for this?...

It seems sort of confusing that the Transform component deals with hierarchy (parent, childCount, DetachChildren, etc), when it mostly deals with location/rotation/scale, and is a part of the gameObject (which is a container for components and child objects)

I know you can just do gameObject.transform.parent... but that can get annoying when you are storing variables and keeping track of objects and constantly have to switch between gameobject and transform references...

Does anyone know the reason for this? or at least why there is no built-in inherited version to make gameObject.parent work?

Comment
Tomer-Barkan

People who like this

-1 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 Dave-Carlile · Oct 23, 2013 at 08:26 PM 1
Share

This is a discussion question and should probably be asked in the forums rather than here. But, I would imagine it has to do with Unity's component based philosophy. A game object is nothing more than a container - all functionality belongs in the components. That's just a guess though.

avatar image Loius · Oct 23, 2013 at 08:48 PM 1
Share

To hopefully help why Transform deals with hierarchy and location &c:

Location, rotation, and scale are handled with matrices. A matrix is most efficiently created for child objects by "adding" it to the parent object. So a transform would say "draw me, then my children", and pass its matrix along, saving lots of time.

A GameObject, however, has no use for matrices. Since Transform already handles them and all GameObjects must have Transforms, it's redundant to have it in both places and could lead to a situation where a change is made in one place and not the other, causing hard-to-find bugs.

avatar image mdolnik · Oct 23, 2013 at 09:06 PM 0
Share

Thanks Loius, That helps explain things. How do I mark your comment as the correct answer?

1 Reply

  • Sort: 
avatar image

Answer by zombience · Oct 23, 2013 at 08:27 PM

That's a design decision unity made.

If you really want to have that functionality, here's a class that will help you:

 public static class ParentFinder
 {
     
     public static Transform parent(this GameObject obj)
     {
         return obj.transform.parent;
     }
 }


include the file ParentFinder.cs in any project you make, and now all gameObjects will have an accessible method called "parent" which will return the gameobject's transform's parent.

If you're going to be doing a lot of this per frame (like flocking hundreds of objects or something), you'll want to create a direct reference to the transform and the transform's parent.

Comment
Jamora
Loius
mdolnik

People who like this

3 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 Jamora · Oct 23, 2013 at 08:51 PM 1
Share

This is called an extension method, in case the OP wants to read up on them.

avatar image mdolnik · Oct 23, 2013 at 09:09 PM 0
Share

Thanks, that method may be useful. I know there are many options for work-a-rounds, I was more curious on the design decisions by Unity... I guess this should have been posted in a discussion.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

18 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 avatar image avatar image avatar image

Related Questions

How can I access a Collider's GameObject in script? 1 Answer

Properly Rotating Child Objects by Script 1 Answer

How to change game object's parent in hierarchy without changing it's transform's position? 1 Answer

Getting instance of an sub object rather than the original's subobject 0 Answers

Parenting from code doesn't really change the hierarchy 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