Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Scaling parent object but not children!

Discussion in 'Scripting' started by emmy21, May 23, 2024.

  1. emmy21

    emmy21

    Joined:
    Nov 16, 2023
    Posts:
    19
    I have multiple scenes that use different assets (prefabs) downloaded from the internet. In a few of the scenes im working on, I want to programmatically change the scale of these prefabs at runtime. In the editor, I set them as a child of a empty game object with scale (1,1,1). The scaling looks and feels fine even programmatically at runtime. But then back in the editor i make some adjustments and modify some seemingly unrelated things (materials, positions of game objects, etc.) and all of a sudden the programmatic scaling breaks. Some things in the prefab scale normally and other things in the SAME prefab dont. But the editor scaling is perfectly normal - the children scale as they should. Im a noob so I think im overlooking something obvious. What in the world is happening?

    Heres some logs. Both balls are in the prefab which is a child of sceneRoot. ball looks as if it scales normally (changing size and position), but ball2 looks to be the exact same size and in the exact same position as it was before. But it IS scaling properly based on the logs, right?

    Code (CSharp):
    1. public void OnScaleSliderChanged()
    2. {
    3.     sceneRoot.transform.localScale = new Vector3(scaleSlider.value, scaleSlider.value, scaleSlider.value);
    4.  
    5.     Debug.Log("sceneRoot Position: " + sceneRoot.transform.position);
    6.     Debug.Log("sceneRoot local Scale: " + sceneRoot.transform.localScale);
    7.     Debug.Log("sceneRoot lossy Scale: " + sceneRoot.transform.lossyScale);
    8.     Debug.Log("ball Position: " + ball.transform.position);
    9.     Debug.Log("ball local Scale: " + ball.transform.localScale);
    10.     Debug.Log("ball lossy Scale: " + ball.transform.lossyScale);
    11.     Debug.Log("ball2 Position: " + coloredBall.transform.position);
    12.     Debug.Log("ball2 local Scale: " + coloredBall.transform.localScale);
    13.     Debug.Log("ball2 lossy Scale: " + coloredBall.transform.lossyScale);
    14. }
    sceneRoot Position: (38.30, 0.39, -91.60)
    sceneRoot local Scale: (1.04, 1.04, 1.04)
    sceneRoot lossy Scale: (1.04, 1.04, 1.04)
    ball Position: (14.68, 11.74, -76.07)
    ball local Scale: (6.73, 6.73, 6.73)
    ball lossy Scale: (7.01, 7.01, 7.01)
    ball2 Position: (35.90, -0.02, -73.90)
    ball2 local Scale: (1.00, 1.00, 1.00)
    ball2 lossy Scale: (1.24, 1.17, 1.46)

    sceneRoot Position: (38.30, 0.39, -91.60)
    sceneRoot local Scale: (2.01, 2.01, 2.01)
    sceneRoot lossy Scale: (2.01, 2.01, 2.01)
    ball Position: (-7.24, 22.26, -61.67)
    ball local Scale: (6.73, 6.73, 6.73)
    ball lossy Scale: (13.51, 13.51, 13.51)
    ball2 Position: (33.67, -0.39, -57.49)
    ball2 local Scale: (1.00, 1.00, 1.00)
    ball2 lossy Scale: (2.38, 2.26, 2.82)
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,922
    Changing the localScale of the parent will not affect the localScale of children. That‘s why it‘s called „local“.

    The lossyScale on the other hand is an approximation. Be sure not to read and apply it anywhere.
    See manual: https://docs.unity3d.com/ScriptReference/Transform-lossyScale.html

    Then there is the question what you mean by prefab? Probably a prefab instance, not the prefab asset itself? If you modify the former without applying the changes, then instantiate the prefab, those changes will not be reflected in the new instance.
     
  3. emmy21

    emmy21

    Joined:
    Nov 16, 2023
    Posts:
    19
    Its cause they were static