- Home /
Question by
RealMTG · Apr 16, 2015 at 06:34 PM ·
uiinstantiate
UI elements is not working correctly with layouts when instantiated
Hi
I am trying to make a grid layout of some UI elements I instantiate by code and of some reason it fails to do so. When I instantiate the object I put it in the object with the grid layout component and of some reason the object doesn't "exists". When I say exists I mean that it is in the scene, has all the components but doesn't show up. If I drag the prefab of the object in before I run the code it works just fine.
Here's my code
foreach (DeskstopIcons icon in desktopIcons)
{
GameObject aIcon = Instantiate(desktopIconPrefab) as GameObject;
aIcon.name = icon.name;
DesktopIcon di = aIcon.GetComponent<DesktopIcon>();
di.name = icon.name;
di.icon = icon.icon;
di.ApplyChanges();
aIcon.transform.SetParent(desktopIconsHolder.transform);
Debug.Log("Done with object");
}
I can't see anything wrong with it.
Thanks in advance
Comment
Best Answer
Answer by RealMTG · Apr 17, 2015 at 02:37 PM
I just noticed that the scale of the UI element was set to 0, 0, 0 of some reason. So I added this code:
aIcon.GetComponent<RectTransform>().localScale = new Vector3(1, 1, 1);
It basically just sets the scale to 1, 1, 1 so it shows up.
Your answer
