• 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
4
Question by dlerozeun · Aug 05, 2010 at 07:58 AM · gameobjecttransform

Why game objects have necessarily a transform component?

It happens that sometimes a monobehaviour does not need any transform. So why is it not possible to remove this component? It would also be possible to create "groups" of game objects (in the scene hierarchy) under another game object which would not have a transform component. This would allow quicker access to gameobjects by the user as the scene hierarchy would be cleaner.


Edit (To Cyclops answer):

Ok Cyclops, actually I wanted to answer to SpikeX and Jashan, editing my original question is not very understanble IMHO. And i can't answer to two persons at the same time in a comment (also the number of characters is limited in a comment!? oO )

Anyway, going back to my issue, if I have a script on a gameobject that want to reach its root gameobject for any reason (like access to a particular monobehaviour on the root), I would use transform.root. However, if this root gameobject is grouped under an empty gameobject. Transform.root would not work any as it would return the group gameobject. To prevent this issue, I originally wanted to remove the transform component of the gameobject used to build the group.

Comment
Add comment · Show 4
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 Mike 3 · Aug 06, 2010 at 11:18 AM 0
Share

Ins$$anonymous$$d of using transform.root, you could just add a root variable to your scripts which you can drag the actual root to? Then you'll be speeding up access to it massively as a bonus

avatar image dlerozeun · Aug 06, 2010 at 12:14 PM 0
Share

Yes, it is what I do. I don't access to the root gameobject each frame with transform.root. I do it, in the Awake or Start method and initialize the so called root variable. But to initialize this variable I have to at least find the root gameobject once and I do that using "transform.root".

avatar image Cyclops · Aug 06, 2010 at 02:08 PM 0
Share

@dletozeun, I understand, but the fact remains that Answer fields are meant only for direct answers to the initial question, not on-going discussions, sorry. UnityAnswers is different in structure from a Forum, and that's part of it - it is purely one Question -> multiple possible Answers. With comments tacked on.

avatar image dlerozeun · Aug 06, 2010 at 04:23 PM 0
Share

Ok I understand Cyclops.

3 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by jashan · Aug 05, 2010 at 11:40 AM

Actually, the reason why this isn't possible is because GameObjects have no hierarchy in themselves. The hierarchy is provided through the Transform components. See also Transform.parent, Transform.childCount and Transform.IsChildOf(...).

The documentation of the Transform class reveals that you also can easily iterate over all the children of a Transform (i.e. the child game objects): Transform

C#:

foreach (Transform child in transform) {
    child.position += Vector3.up * 10.0;
}

UnityScript:

for (var child : Transform in transform) {
    child.position += Vector3.up * 10.0;
}

So, while you really don't need the spatial information in Transform for groups of game objects that have and need no spatial reference, you need the transform to be able to put the GameObjects into the hierarchy of the scene. Without them, this simply wouldn't work.

That's why in Unity, every GameObject always has a Transform component attached to it.

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

Answer by qJake · Aug 05, 2010 at 08:12 AM

Unity's system is designed such that every object in the world must have a position, rotation and scale, even if it isn't visible. This is so that, for example, if you wanted to code a spawnpoint system, you could use an empty game object, but still retrieve its position.

The transform component is "hard-wired" into every game object, and is required to be there. This has absolutely no adverse effect on game performance. You can't remove them, and they won't interfere with your game's performance, so don't worry about them.

Comment
Add comment · Show 3 · 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 Xarbrough · Feb 04, 2017 at 12:18 AM 1
Share

I know this thread is old, but people might be still be reading this kind of false information. Having empty transform can have a very noticeable impact on your games performance. If you group a thousand empty transforms under each other, you will be able to see the effect in the profiler. Every call to position, rotation or scale will have to traverse the entire hierarchy when doing the internal matrix transformations. This is also not a contrived example. Our current game has 30,000 objects in a scene and at least 6000 are empty "folder" transforms only used to group things visually for our designers. Removing those benefits the final framerate.

avatar image Kainui Xarbrough · Jul 13, 2017 at 03:14 AM 0
Share

I'm just curious then, how do you go about removing all these "folder" transforms, since I know you're not going to manually remove them before build time. Is there some nice way of doing this? $$anonymous$$y guess is you tag all these objects to be removed as "folder" and do some kind of script to delete them all but that seems like that'd end up deleting at run time ins$$anonymous$$d of compile time, so that can't be the best way to do it.

avatar image Xarbrough Kainui · Jul 13, 2017 at 09:29 AM 0
Share

I'd personally go for 'Folder' Tag or something, that way during build time (via Postprocessbuild or Postprocessscene) you can find all those GameObjects, flatten the hierarchy and delete them. In our case back then, we missed the opportunity to tag all folder objects, ins$$anonymous$$d I had to traverse the hierarchy to find all leaf objects (deepest children), then walk back up and check, if the parent has no components (and is probably just a folder), then I can delete it and take the children one level higher. Sometimes, code references the parent object, so in fact, it's not only a folder, but a spawn point or pivot. We knew most of these cases, so I could just hardcode them in. You could also search for public fields via reflection (on all objects in the scene) and create a hashtable that stores, which objects are referenced, and if a potential folder is, don't delete it. This might still break for edge cases like the use of GameObject.Find or GetComponentInParent, but most of these I say, shouldn't be used anyway, because they are so obscure.

avatar image
-3

Answer by dlerozeun · Aug 05, 2010 at 12:37 PM

Ok thank you for your replies spikeX and Jashan.

I understand that unity encapsulated the hierarchy concept inside the Transform component. Actually, we would like to group gameobjects for visual convenience in the scene hierarchy interface (and secondly, speed up gameobject finding operations). The problem is when we do that, if a gameobject monobehaviour inside a group access to the transform.root it will get the empty gameobject used to group other gameobjects because it has a transform and that's not what we want.

So, actually it is not gameobject that we need, just a "group object" to better organize the scene view and quickly access to game objects. It is the same concept as namespaces in C++ or C#... And by removing the transform of a gameobject we wanted to simulate this feature.

Comment
Add comment · Show 1 · 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 Cyclops · Aug 05, 2010 at 02:06 PM 1
Share

$$anonymous$$inor point - you should edit your original Question with this update, ins$$anonymous$$d of putting it in an Answer. Having said that - using a top-level GameObject to group things is a workable solution. And a $$anonymous$$onobehavior gameobject does have access to its own Transform, so I'm not sure what you are trying to do with transform.root? Do you want a specific $$anonymous$$onobehavior to have access to all the other objects grouped up? That's easily doable. Basically, it would help if you further specified what your actual goal is. You can edit the original Question for that.

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

2 People are following this question.

avatar image avatar image

Related Questions

Control Ship and Gun Separately 2 Answers

use list of gameobjects transform in raycast 0 Answers

Transform values not exact 2 Answers

Moving a GameObject to a certain point in world space via script 1 Answer

Transform - GameObject? 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