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:
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.