0
\$\begingroup\$

In Unity I created a Panel with 3 children (An UI Image (LogoAch), and 2 TextMeshPro components (Name, Progress)).

It looks something like down below:enter image description here

I saved this as a prefab and I try to instantiate it through code. Problem is for whatever reason the UI Image is not ?rendered? in the instantiated version? I have this short video showcasing the problem:

https://www.youtube.com/watch?v=RTa_noJy4bE

Does anyone have any hunch of what exactly is the problem?

My code is the following:

// instantiate the popup
GameObject achPopup = Instantiate(prefabAchievementsPopup, transform.position, transform.rotation);

// set it as a child of Canvas
achPopup.transform.SetParent(canvas.transform);

// set Pos Z to 0
// set Left, Bottom to 0, 300
// set Right, Top to 0, -300
// set Scale to (1,1,1)
achPopup.GetComponent<RectTransform>().localPosition = new Vector3(1000, 1000, 0);
achPopup.GetComponent<RectTransform>().offsetMin = new Vector2(0, 300); //left, bottom
achPopup.GetComponent<RectTransform>().offsetMax = new Vector2(0, 300); //right, top
achPopup.GetComponent<RectTransform>().localScale = new Vector3(1, 1, 1);

// set new sprite in Logo, new text in Name, Progress
achPopup.transform.Find("LogoAch").GetComponent<Image>().sprite = circleOfLifeLogo; // [SerializeField] private Sprite circleOfLifeLogo;
achPopup.transform.Find("LogoAch").GetComponent<Image>().color = circleOfLifeLogoColor; // [SerializeField] private Color circleOfLifeLogoColor;
achPopup.transform.Find("Name").GetComponent<TMPro.TextMeshProUGUI>().text = "Circle of life";
achPopup.transform.Find("Progress").GetComponent<TMPro.TextMeshProUGUI>().text = "1/1";
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You're assigning your image a colour with zero alpha (100% transparent).

Compare the working example in your video:

Colour swatch with white bar
(White bar under colour swatch = 100% alpha)

...with the non-working example:

Colour swatch with black bar
(Black bar under colour swatch = 0% alpha)

So go to the inspector where you've configured circleOfLifeLogoColor and ensure you give it a higher alpha value to get an opaque image.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you! I did not notice the alpha value was zero! \$\endgroup\$
    – Stefan
    Oct 1, 2021 at 14:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .