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… :.(

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!