- Home /
How does Transform.SetParent() worldPositionStays work with null parent?
Hi there,
I have trouble understanding how Transform.SetParent()
's worldPositionStays
parameter works, specifically when parent is set to null
.
The scripting reference says:
If true, the parent-relative position, scale and rotation are modified such that the object keeps the same world space position, rotation and scale as before.
Naturally, I usually set it to true
, because I don't want parenting to change position, rotation, or scale. So I was surprised when trying to "unparent" with transform.SetParent(null, true);
the inverse of the parents' scale was applied! Even though if I carefully think about it, localScale
relative to null
parent (assuming the scale of null
is (1, 1, 1)) would mean just localScale
, I still expected that it will automatically compensate for the relative differences between the original and new parents' scales. Note that if I "unparent" with worldPositionStays = false
it works as I originally expected.
EDIT: Interestingly, this answer says the opposite about how worldPositionStays
works...
So, basically, my questions are:
- why does transform.SetParent()
's worldPositionStays
work this way when the parent is set to null
?
- is the above observed the intended behaviour, or is it a bug? (I was expecting it to compensate for scale differences)
- in general, what is the recommended way of using worldPositionStays
?
Also, I seem to remember transform.parent
being deprecated in Unity 5 and worldPositionStays
defaulting to false
... does my memory play tricks on me, or was this changed recently?
I don't know but if I had to guess you'd be the one to find this answer...
I am sorry.. :.(
Answer by tormentoarmagedoom · Apr 25, 2018 at 09:59 AM
Good day.
If i had this problem, i should solve the problem "thinking outside the box"... i mean, you can store the values you need, unparent it, and assign the values again (if unparentiung changes it, it doesent care, you wil lchange it after)
Bye!
Of course I can assign it the values I want, but that is not "thinking outside the box", that is a workaround, and I want to understand how it works internally.
To give a bit of background, the whole issue came up while writing an object pool to make instantiating more efficient. When I moved an object to the pool, I parented it to the object pool game object, and when taking it out, I unparented ( SetParent(null)
). But it seems setting the parent is really slow, and modifying position and scale slows things down even more.
Your answer
Follow this Question
Related Questions
How to make cubes between 2 point and stick them together? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Rotate Parent using Child as Pivot 0 Answers
Limit & Scaling Translation Rate. 0 Answers