- Home /
The question is answered, right answer was accepted
Instantiated UI prefab in canvas does not render image
I'm trying to make a radar that marks game objects with a certain component with an "icon" UI prefab. When it comes to positioning it in the right place it seems to work, but the image component does not show up, although the game object is a child (of a child) of the canvas. I tried parenting it to the canvas directly (although it's undesireable because of the way I set up the radar), but no luck. I also tried creating the game object from scratch rather than using a prefab, still no luck.
For now I'm only testing a single icon, the player's. Radar icons are created as the gameobjects are created. This is how Unity looks like when I start the game:
The diamond sprite is quite small relatively to the "Radar" donut so even if it rendered you might not see it, but trust me, it's not there.
This is the relevant code, it's inside Start() (i tried putting it in Awake() and it makes no difference) and it instantiates the icon and sets the required paramenters, depending on what pre-defined radar icon I want to use (the commented part would generate the icon object from scratch):
DefaultRadarIcon radarIconRef = GlobalVars.Instance.defaultRadarIcons[(int)radarIcon];
GameObject instance = Instantiate(rdricon, radarTransform); //GlobalVars.Instance.canvas.transform
rectTransform = instance.GetComponent<RectTransform>();
rect = rectTransform.rect;
image = instance.GetComponent<Image>();
/*
GameObject radarGO = new GameObject("radar_icon (" + gameObject.name + ")", typeof(RectTransform));
rectTransform.SetParent(radarTransform, false);
rectTransform = radarGO.GetComponent<RectTransform>();
rect = rectTransform.rect;
image = radarGO.AddComponent<Image>();
*/
rect.width = radarIconRef.sideLength;
rect.height = rect.width;
image.color = radarIconRef.color;
image.sprite = radarIconRef.icon;
Another thing is that setting the rectangle's width/height doesn't seem to work, it stays to whatever was the previous value (which would be 100x100 for the "from scratch" gameobject, and 12x12 for the prefab)
Thanks in advance for any insight, if I missed something I should've told feel free to ask.
Answer by xxmariofer · Feb 06, 2019 at 11:59 AM
hello, chanfe the rectwidth and height line for this
rectTransform.sizeDelta = new Vector2(radarIconRef.sideLength,radarIconRef.sideLength);
this will for sure scale the rect, if it doesnt is a problem with radariconref. also 12x12 is really really little, since i imgine is atleast 800 width canvas, can you share screenshot of the canvas inspector and game view?
Your code seems to work. 12x12 is just a test value, it might not be optimal but it would be at least seeable, if the image rendered.
The canvas inspector:
The game view, at this time there is a game object with a 24x24 orange diamond image at the center of the top-left donut, which clearly isn't rendering.
EDIT: might as well also show the icon's inspector:
hello, you can not see the image because it has no alpha, not sure were you are setting up radarIconRef.color but his alpha is 0.
...now that's embarrasing. The default value for the Color class is probably (0,0,0,0) and I just changed the RGB values -_-
Thanks for your time I guess lmao