Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
3
Question by Inok · Mar 11, 2016 at 10:29 AM · transform.parent

transform.parent vs transform.SetParent

I can make target transform to be child of another transform using 2 variants:

 1) Go.transform.parent = transform;
 2) Go.transform.SetParent(transform);

So i not sure what variant better (in performance perspective).

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

5 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by _invalidusername · Mar 11, 2016 at 02:56 PM

SetParent allows you keep the transforms local orientation rather than its global orientation (by setting the second parameter worldPositionStays to true). In terms of performance I would imagine this makes SetParent slightly slower, but the difference would be negligible

See the docs for Transform.parent and for Transform.SetParent, particularly the line :

worldPositionStays - If true, the parent-relative position, scale and rotation is modified such that the object keeps the same world space position, rotation and scale as before.

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 HansCronau · Nov 16, 2016 at 11:48 AM 0
Share

I think you may have made a mistake in your comment. As I understand it, worldPositionStays should be set to false ins$$anonymous$$d of true to retain the local orientation.

avatar image syllabusgames · Sep 29, 2021 at 08:33 PM 0
Share

I just tested performance with 10,000,000 iterations each. SetParent(,true) was 1% faster than .parent SetParent(,false) was 3% faster than .parent I wouldn't worry about efficiency and use SetParent just so you don't have to see the warnings.

avatar image
5

Answer by troien · Mar 11, 2016 at 10:55 AM

transform.SetParent is relatively new (I believe sinse Unity UI came out) and exists because it also has an overload with a second boolean argument. And they also made a version which doesn't have this boolean second argument (which defaults to having the second argument set to true). Setting the property and calling the method with one argument or with the second argument set to true all do exactly the same, but the method has a different behaviour when you call it like this.

 Go.transform.SetParent(transform, false);

There really shouldn't be any (noticeable) performance difference. The method probably has one extra method call because it calls setparent again with the second argument set to true but as I said, it shouldn't be noticeable.

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 manav907 · Jun 29, 2019 at 12:06 PM 0
Share

Thank you, I don't know if you are aware of it but giving that argument to false makes it so that object usees parent as origin at the time of instantiating. this was something I was struggling with while I was making my first game today. I don't know if the problem that I faced was a bug or a slip of code but using instantiatedSpawnner.transform.SetParent(floorSet.transform,false); fixed my problem exactly how i wanted it to be fixed.I can quite describe what it does exactly so if you have free time just try it out

var instantiatedSpawnner = Instantiate(floor, transform.position, Quaternion.identity); instantiatedSpawnner.transform.SetParent(floorSet.transform,false);

avatar image troien manav907 · Jun 30, 2019 at 06:46 PM 0
Share

I'm aware of what the second parameter does, I didn't mention it because I focussed on the difference in performance as the question was about that. I also already linked the docs, which describe the second argument of SetParent in detail. But actually mentioning what the difference is might have made my answer better, yes :p

avatar image
0

Answer by ibx00 · Feb 19, 2020 at 10:01 AM

Transform.setparent: Help set the local position of the gameobject with the help of an argument i.e. worldPositionStays. If true, then the position it is carrying in its transform becomes its global position wrt world. And if false, then its position is according to its parent behaving like a world. It means the parent position will be taken as the origin for the child object.

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
0

Answer by mrmatt1877 · Dec 30, 2020 at 06:06 PM

parent = gameobject keeps the worldposition of the child while SetParent allows you the choice between world position or changing the position to be relative to the parent. When the object is made a child of a parent in world space the object will stay in the same place it was when nested but the position, rotation, and scale values will still be modified to be relevant to the parent. You can see that in the demonstration here https://www.monkeykidgc.com/2020/12/tips-and-tricks-unity-set-parent.html

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
0

Answer by Regretful123 · Mar 12 at 04:56 PM

Just wanted to add an additional information here. Unity's Instantiate() also includes the parameters to assign new parent to the object you're about to create.

Either one of these two API will create a new game object assign to the target parent transformation.

Instantiate( prefabTemplate, parent.transform ); Instantiate( prefabTemplate, Vector3.zero, Quaternion.Identity, parent.transform );

Both of these will save the additional cpu cost of applying parent transformation to your newly created game object. SetParent and parent assignment have a significant performance cost when applying transformations.


See below for more information: Unite Berlin 2018 - Unity's Evolving Best Practice Unity Doc's - Object.Instantiate

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

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

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

Related Questions

what object does the parent child code go on? 1 Answer

[Deep Profiling] Confusion in `transform.parent = null` 0 Answers

Carrying physical game objects in javascript scripting problems with Unity 5. 2 Answers

Understanding root vs parent 1 Answer

Player hiding inside 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