I am trying to show a popup to a user once he is defeated. Part of the popup prefab is a dimmed screen with a stretch anchor. When I spawn the popup prefab using Event system and I mount it to my Canvas, it doesn't stretch. Anchors remain at 0,0,0 (middle). Any idea why or how can I fix it?
For UI mounting, you'll usually want to not keep the current world position and would use this to mount it:
transform.SetParent(parentTransform, false);
transform is the Transform of the game object you want to mount, parentTransform is what you want to mount it to.
If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
Perhaps I could pass the parent name and then find the GameObject from within the script. I am just not sure whether that's the best possible way.
public string GVKey_ORKUICanvasTag = "UICanvasTag";
private string UICanvasTag;
private GameObject UICanvas;
// Start is called before the first frame update
void Start()
{
UICanvasTag = ORK.Game.Variables.GetString(GVKey_ORKUICanvasTag);
UICanvas = GameObject.FindGameObjectWithTag(UICanvasTag);
resetTransformOffset();
SetParent(UICanvas);
}
private void resetTransformOffset()
{
RectTransform rectTransform = GetComponent<RectTransform>();
rectTransform.offsetMin = new Vector2(0,0);
rectTransform.offsetMax = new Vector2(0, 0);
}
private void SetParent(GameObject parent)
{
transform.SetParent(parent.transform, false);
}