Unity sizeDelta height returns incorrect value

unity sizedelta vs rect
unity recttransform width
recttransform lookat
unity set sizedelta
recttransform transformpoint
image recttransform sizedelta
unity recttransform scale
unity recttransform parent

I have this RectTransform:

Its part of my hover GUI. Everytime I hover over something the GUI is filled with information (because of this the height is dynamic) and moved to the object I hover over.

Sometimes the GUI is offscreen and to move it back into view I need to know the current height.

In the image above is an example, there the current height is 545.01, but my script logs a different height:

RectTransform hoverElementRect = instance.hoverGUI.GetComponent<HoverGUI_Components>().GetHoverElement().GetComponent<RectTransform>();

Debug.Log(hoverElementRect.sizeDelta); // gives (450, 842)

How is this possible?

I leave it for now without too much code, I am a newb in unity and maybe someone already has an idea, but here is the whole GUI postioning and creating code.

The relevant part is at the end.

EDIT:

foreach (RectTransform rect in instance.hoverGUI.GetComponent<HoverGUI_Components>().GetHoverElement().GetComponentsInChildren<RectTransform>()) {
    LayoutRebuilder.ForceRebuildLayoutImmediate(rect);
    rect.ForceUpdateRectTransforms();
}

RectTransform hoverElementRect = instance.hoverGUI.GetComponent<HoverGUI_Components>().GetHoverElement().GetComponent<RectTransform>();

LayoutRebuilder.ForceRebuildLayoutImmediate(hoverElementRect);
hoverElementRect.ForceUpdateRectTransforms();

ForceUpdateRectTransforms() does not cahnge anything. I have even used it on all rects in the whole GUI object. I even tried to use it after each operation I have done.

You can fix your specific issue by using LayoutUtility.GetPreferredHeight(rectTransform). Don't force update anything.

Here's an extension method that may be helpful generally. You may need to reorder the checks for your specific application.

public static class RectTransformExtension
    {
        /// <summary>Sometimes sizeDelta works, sometimes rect works, sometimes neither work and you need to get the layout properties.
        /// This method provides a simple way to get the size of a RectTransform, no matter what's driving it or what the anchor values are.
        /// </summary>
        /// <param name="rectTransform">The rect transform to check.</param>
        /// <returns>The proper size of the RectTransform.</returns>
        public static Vector2 GetProperSize(this RectTransform rectTransform) //, bool attemptToRefreshLayout = false)
        {
            Vector2 size = new Vector2(rectTransform.rect.width, rectTransform.rect.height);

            if (size.x == 0 && size.y == 0)
            {
                size.x = LayoutUtility.GetPreferredWidth(rectTransform);
                size.y = LayoutUtility.GetPreferredHeight(rectTransform);
            }

            if (size.x == 0 && size.y == 0)
            {
                LayoutGroup layoutGroup = rectTransform.GetComponent<LayoutGroup>();

                if (layoutGroup != null)
                {
                    size.x = layoutGroup.preferredWidth;
                    size.y = layoutGroup.preferredHeight;
                }
            }


            return size;
        }
    }

RectTrasform SizeDelta value isn't correct at different , ref res: 1920x1080 - screen match mode: scale with width or height But I can't get the correct value because the SizeDelta is wrong. At ANY resolution rect. width returns 256, sizeDelta return 256, and RectTransformUtility. Returns the component of Type type if the game object has one attached, null if it doesn't. GetComponentInChildren: Returns the component of Type type in the GameObject or any of its children using depth first search. GetComponentInParent: Returns the component of Type type in the GameObject or any of its parents. GetComponents

I know that this question is already old but I'll share my simple solution anyway: Make sure that you have added "using UnityEngine.UI" namespace to your script. Otherwise, RectTransform.sizeDelta will return a wrong value.

UI RectTransform.rect.width/heigth returns incorrect/negative values , Use Unity to build high-quality 3D and 2D games, deploy them across mobile, desktop, VR/AR, consoles or the UI RectTransform.rect.width/heigth returns incorrect/negative values But I can't correctly set/get values from rect because " width"/"height" and .sizeDelta returns me something like 3.0518e-05. Natty. 11:00 AM [ Natty] codeigniter Enable post method in codeignitor {if i try GET method its goto action page but not redirected} By: Ajay Salunkhe 3.5; Natty] codeigniter Enable post method in codeignitor {if i try GET method its goto action page but not redirected} By: Ajay Salunkhe 3.5

Yeah, always use

hoverElementRect.rect.size

instead of

hoverElementRect.sizeDelta

How do I get the literal width of a RectTransform?, This returns a negative value in my case (because of anchoring). as: rect: (x: 0.00, y:36.00, width:0.00, height:-72.00) sizeDelta: (0.0, -72.0). Hey, Need some math help. Lets say there is a square sized A(1000w,1000h). it is represented in Unity UI in a Rect of x,y,w,h 0,0,800,800 now imagine this square A is divided into 2.

Scripting API: RectTransform.sizeDelta, If the anchors are together, sizeDelta is the same as size. If the anchors are in each of the four corners of the parent, the sizeDelta is how much bigger or smaller� * more checks if access token is set * Style development - colorization and atlas template generator * add uv mapping height modifier and a demo scene to show it off * fix TextureSideWallModifier to respect min_height property * fix a bug in wall segment calculation in TextureSideWallModifier where CenterSegment feature yielded long first

Scripting API: RectTransform, sizeDelta, The size of this RectTransform relative to the distances between the GetComponent, Returns the component of Type type if the game object has one� I need help with my script. The purpose of the script is to rotate a gameobject that is a child of the player and have it follow the mouse. To do this, I use this script; ` void Update { var mouse = Input.mousePosition; var screenPoint = Camera.main.WorldToScreenPoint(transform.localPosition); var offset = new Vector2(mouse.x - screenPoint.x, mouse.y - screenPoint.y); var angle = Mathf.Atan2

Correctly get size from Canvas UI element on Unity, Try doing all UI layout with no scaling, setting only the RectTransform's anchored position, size delta and anchors for position, and only relying� While the profiler is closed, Unity takes about 900-1000 MB of memory while in play mode. While the profiler is open Unity starts taking about 10,000 MB of memory in play mode, and this number continues to rise as play mode runs for longer. Unity takes up an additional 30-70 MB of memory every second that play mode is left running.

Comments
  • Likely local vs. global
  • sizeDelta as the name says is a local delta offset to the parent object. rather try using Debug.Log(hoverElementRect.rect.width + ", " + hoverElementRect.rect.height); which afaik should return the absolute sizes.
  • I Check this method and it returns same value for me so Maybe this is not that RectTransform and your code gets another RectTransform. define a public RectTransform field and set it in unity editor and check its sizeDelta to see what is going on. or print the name of its GameObject .
  • SizeDelta Description: The size of this RectTransform relative to the distances between the anchors. If the anchors are together, sizeDelta is the same as size. If the anchors are in each of the four corners of the parent, the sizeDelta is how much bigger or smaller the rectangle is compared to its parent.
  • rect.size gives me the same values... well I have no clue were the problem could be.