Unity: UI prefabs go invisible when i Instantiate it

Hello,

I have a custom UI element in the Mainmenu scene that works fine, but when i make a prefab out of it, all the text and images become invisible, and the rect transform goes blank

If i Duplicate it with ctrl-D, it’s fine, but copy-pasting or dragging the prefab leaves it blank, even if al the layout elements are the same

I need to pass this pop-up window to another scene, but it doesnt even work in the same scene

The orginal prefab:


The Instantiated, invisible version


Here is the script attached to the object:

public class QuestionPopUp : MonoBehaviour
{
    [SerializeField]
    private TextMeshProUGUI questionText;
    [SerializeField]
    private Text yesButtonText;
    [SerializeField]
    private Text noButtonText;
    [SerializeField]
    private Button yesButton;
    [SerializeField]
    private Button noButton;

    public delegate void PressYesDelegate();
    public PressYesDelegate OnPressYes;

    public delegate void PressNoDelegate();
    public PressNoDelegate OnPressNo;

    public void Start()
    {
        questionText = GetComponentInChildren<TextMeshProUGUI>();
    }

    public void OnPopup(QuestionPopupInfo info)
    {
        try
        {
            OnPressYes += info.OnYes.Invoke;
        }
        catch (System.ArgumentException) { Close(); }
        OnPressYes += info.OnYes.Invoke;
        try
        {
            OnPressNo += info.OnNo.Invoke;
        }
        catch(System.ArgumentException) { }
        OnPressNo += Close;
        gameObject.SetActive(true);
        questionText.text = info.questionText;
        yesButtonText.text = info.YesButtonText;
        noButtonText.text = info.NoButtonText;
    }

    public void OnButtonPress()
    {
        ClearDelegates();
        Close();
    }

    public void Close()
    {
        ClearDelegates();
        gameObject.SetActive(false);
    }

    public void PressYes()
    {
        OnPressYes?.Invoke();
        Close();
    }

    public void PressNo()
    {
        OnPressNo?.Invoke();
    }

    public void ClearDelegates()
    {
        OnPressYes = null;
        OnPressNo = null;
    }
}

In your second screenshot, try moving the prefab in your object hierarchy so that it is a child of your Canvas.

1 Like

Thanks it worked