RectTransform's localScale not changing

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:

GameObject ThisRoomPanel = new GameObject(index.x.ToString() + index.y.ToString());
            ThisRoomPanel.AddComponent<Room>();
            ThisRoomPanel.AddComponent<RectTransform>();
            ThisRoomPanel.AddComponent<Image>();
            ThisRoomPanel.GetComponent<RectTransform>().anchoredPosition = new Vector3((index.x * 50) + 25, (index.y * -50) - 25, 0);
            ThisRoomPanel.GetComponent<RectTransform>().sizeDelta = new Vector2(50, 50);
            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

Any reason why you can’t just use a prefab and have to use this unhappy looking code instead?

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?

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:

public void MakeUI()
{
    GameObject panel = new GameObject("SomeUI");
    RectTransform rectTransform = panel.AddComponent<RectTransform>();
    panel.AddComponent<Image>();
    rectTransform.anchoredPosition = new Vector3(50, 50, 0);
    rectTransform.sizeDelta = new Vector2(50, 50);
    rectTransform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
}

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.

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:
8519594--1136084--upload_2022-10-17_6-36-30.png
8519594--1136087--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?

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.