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?
  • Regular mounting and UI mounting works slightly different, so using the event system probably doesn't really result in the same behaviour as when you'd just drag in on the canvas in your scene hierarchy.

    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.
    Please consider rating/reviewing my products on the Asset Store (hopefully positively), as that helps tremendously with getting found.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • Thank you. Would you mind suggesting how can I achieve that in the most elegant way? Calling it at the spawn prefab? In the start function of the prefab script component?
  • edited February 2021
    Trying it now, to pass the parentTransform from the event, but it seems like it can pass only Vector3 variables. Any ideas?

    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.
    Post edited by zatokar on
  • I had to do all this, but I made it to work. Thanks a lot for your help!

    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);
    }
Sign In or Register to comment.