Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Scaling issue with parenting gameobjects

Discussion in 'Scripting' started by randomperson42, Feb 6, 2015.

  1. randomperson42

    randomperson42

    Joined:
    Jun 29, 2013
    Posts:
    974
    In a script I have to "parent" and "un-parent" GameObjects in a scene while changing position and rotation. Currently my code looks something like:
    Code (CSharp):
    1. transform.parent = null;
    2. transform.position = whateverVector3;
    3. transform.rotation = whateverQuaternion;
    4. transform.parent = whateverTransform;
    So I'm clearing the parent so it's no longer a child of any Transform, then setting the position and rotation, then setting the parent of the object to whatever new Transform I want. This is so that I don't get unexpected scales when it's set as a child of other Transforms that might have different scales.

    That works fine, but here's the problem. When my GameObjects get set as a child of certain other GameObjects, they will sometimes (not always) have a much larger scale than they should. It's hard to identify the problem because the scale in the inspector shows what it should be.

    In other words, lets say the transform should have a scale of 1. While the game is running, the transform component in the inspector says that the scale is in fact 1. But it's not. The interesting thing is that when I scale the object manually in the scene, I can just drag the scale handle the slightest amount, and the actual size of the object in-scene will go back to what it should be - in this case a scale of 1. So I went into my script and added this line of code after the position and rotation are set, and before the parent is set:
    Code (CSharp):
    1. transform.localScale = transform.localScale;
    And now it works flawlessly. So what I would like to know is - what is going on? Has anyone seen this before? Why does the size of the object show up in the scene differently than the actual scale until you set the scale to something - even it's own value? I guess there's some function internally that sets the size of the object in-scene to the values in the scale - and that that function didn't get called somehow? Anyway I'd like to know what's going on.

    Hope this all makes sense.
     
  2. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    Try setting localPosition and localRotation then I believe you shouldn't have to tickle the localScale. At my day job, so can't test unfortunately.

    I have run into this myself, but I couldn't explain it properly and I don't have a link to where I read about it before, sorry.
     
  3. randomperson42

    randomperson42

    Joined:
    Jun 29, 2013
    Posts:
    974
    But when I set the position and rotation, there is no parent GameObject.
     
  4. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Use "SetParent" with the second parameter to false (or true, don't remember, I'm kind of busy right now)
     
  5. randomperson42

    randomperson42

    Joined:
    Jun 29, 2013
    Posts:
    974
    As I understand it, SetParent gives you the option to keep world position, which is already happening if I set it manually, or keep local position, which is the opposite of what I want to do. I've looked into it, and I don't think that's what I want.

    Plus SetParent was introduced in 4.6 (I think) and I'm working on an asset which needs to be backward-compatible with prior versions of Unity.