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).
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.
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.
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.
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.
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);
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
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.
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
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
Your answer
![](https://koobas.hobune.stream/wayback/20220612071401im_/https://answers.unity.com/themes/thub/images/avi.jpg)