4
\$\begingroup\$

How do I make it so an object doesn't scale with another object that I parented it with by script? If anyone knows how to get around this I would really appreciate it.

This is the part of the script I used for parenting:

arrowToObjAnchor.transform.parent = hitTransform;
\$\endgroup\$
2
  • \$\begingroup\$ I would also love to know if there's a built-in way to do this; I just rescale the object (usually to Vector3.one) after parenting. \$\endgroup\$
    – jhocking
    Commented Jun 16, 2015 at 16:04
  • \$\begingroup\$ The easiest way to do this, is to not make the object a child. afaik there is no way to ignore the transform changes. \$\endgroup\$
    – jgallant
    Commented Jun 16, 2015 at 16:19

1 Answer 1

4
\$\begingroup\$

I'm assuming your question is about the scale change that occurs at the moment you parent one object to another with a net scale other than (1, 1, 1). This can be avoided, as described below. Any subsequent changes to the parent's scale will still cascade down to affect all children however, so if you want the object to retain its scale regardless of what happens to the parent, your options are either a script that applies a compensating scale to the child with each change, or changing your hierarchy so that the child is not parented to the scaling object (eg. using physics joints to connect their positions & rotations without scale leaking through)

You can use transform.SetParent(parent, false) to keep the object's local position, rotation, and scale unchanged when reparenting it:

public void SetParent(Transform parent, bool worldPositionStays);

Parameters
-------------------------------------------------------------------------------    
parent              The parent Transform to use.
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.

Note that this may cause the object's resulting worldspace position & rotation to change as a consequence of the new parent's transform.

If you only want to keep the local scale, but allow the local position/rotation to be updated as normal, then you can do something like this:

var originalScale = myObj.transform.localScale;
myObj.transform.parent = newParent;
myObj.transform.localScale = originalScale;
\$\endgroup\$
2
  • 1
    \$\begingroup\$ omg is SetParent(parent, false) new?? \$\endgroup\$
    – jhocking
    Commented Jun 16, 2015 at 17:48
  • 2
    \$\begingroup\$ @jhocking I think they introduced it with the new UI features, for RectTransforms, since UI elements are often intended to be sized & scaled relative to whatever parent container they find themselves within. \$\endgroup\$
    – DMGregory
    Commented Jun 16, 2015 at 17:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .