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
-1
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
Add comment · 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

· Add your reply
  • Sort: 
avatar image
3

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
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 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.

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

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

Moving objects together without parenting 5 Answers

Properly Rotating Child Objects by Script 1 Answer

Parenting from code doesn't really change the hierarchy 1 Answer

find next(not necessarily immediate next) child of another parent in hierarchy with tag "checkpoint"? 1 Answer

moving child objects in hierarchy and keep stacks/sorting? Help needed 0 Answers


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