UI elements is not working correctly with layouts when instantiated


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;


            Debug.Log("Done with object");

I can’t see anything wrong with it.

Thanks in advance

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.