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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question RectTransform's localScale not changing

Discussion in 'Editor & General Support' started by spiderhawk1315, Oct 17, 2022.

  1. spiderhawk1315

    spiderhawk1315

    Joined:
    May 20, 2022
    Posts:
    12
    I'm just trying to change the localScale of the RectTransform of a GameObject. No clue why it isn't working, but it looks like this:

    Code (CSharp):
    1. GameObject ThisRoomPanel = new GameObject(index.x.ToString() + index.y.ToString());
    2.             ThisRoomPanel.AddComponent<Room>();
    3.             ThisRoomPanel.AddComponent<RectTransform>();
    4.             ThisRoomPanel.AddComponent<Image>();
    5.             ThisRoomPanel.GetComponent<RectTransform>().anchoredPosition = new Vector3((index.x * 50) + 25, (index.y * -50) - 25, 0);
    6.             ThisRoomPanel.GetComponent<RectTransform>().sizeDelta = new Vector2(50, 50);
    7.             ThisRoomPanel.GetComponent<RectTransform>().localScale = new Vector3(1f, 1f, 1f);
    I can't find much about it online. Just a bunch of people using it wrong, and being told to do it the way I have it
     
    Last edited: Oct 17, 2022
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    Any reason why you can't just use a prefab and have to use this unhappy looking code instead?
     
  3. spiderhawk1315

    spiderhawk1315

    Joined:
    May 20, 2022
    Posts:
    12
    Nothing really, other than I would have to look up how to use a pre-fab. Shouldn't make a difference. Would still have to set the localScale right?
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    Well prefabs are Unity 101 so it'd be worthwhile learning that ASAP.

    Mind you, I tidied up the code some and tested it myself:
    Code (CSharp):
    1. public void MakeUI()
    2. {
    3.     GameObject panel = new GameObject("SomeUI");
    4.     RectTransform rectTransform = panel.AddComponent<RectTransform>();
    5.     panel.AddComponent<Image>();
    6.     rectTransform.anchoredPosition = new Vector3(50, 50, 0);
    7.     rectTransform.sizeDelta = new Vector2(50, 50);
    8.     rectTransform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
    9. }
    And it worked for me (sort of).

    Mind you, mind you, UI generally needs to A: be part of a larger canvas (nothing will render otherwise), and B: you're better of not manually assigning position and scale and letting components such as Grid Layout Group do the work of arranging everything for you.

    So I guess it'd be worth doing some further learning on how to use UGUI and the various components you get out of the box.
     
  5. spiderhawk1315

    spiderhawk1315

    Joined:
    May 20, 2022
    Posts:
    12
    Well I don't know what's up with mine then. I have used prefabs in other projects, but only in 3d ones where I'm just creating primitives.

    Anyway, the problem is that the Grid Layout group is applied to these objects, but they still have an absurd size, and I don't know why. I honestly don't know why they are part of a Grid Layout Group. I don't recall every programming it that way. When they are all made, they look like this in the scene:
    upload_2022-10-17_6-36-30.png
    upload_2022-10-17_6-36-52.png

    I just need them to not have that ridiculous scale. I have no idea where it came from maybe you have some insight into that?
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    Well from what I can only assume, you have a grid layout component on your 'Panel' UI Object most likely (it should only be on that one parent as well). When values are driven by a layout component, you generally also cannot manually drive values because the layout component is overriding all of that. Brute forcing it with code won't change anything.

    So it's either one or the other, and my recommendation is to let the grid layout component do the work for you.