Getting the actual height of content size fitter parent

I have a parent gameobject (Empty) with a content size fitter with horizontal and vertical fit set to preferred size which is also a Horizontal Layout Group with Control Child size width and height enabled:

It has got a TextMeshPro text as child, and now, as the text expands the parent also expands. The problem is, I want to get the height of this parent. And if I use this:

var rect = parent.gameObject.GetComponent<RectTransform>.rect
Debug.Log($"Width: {rect.width}, height: {rect.height}");  or even, var mValue = parent.gameObject.GetComponent<RectTransform>.sizeDelta; Debug.Log($"sizeDeltax: {mValue.x}, sizeDeltaY: {mValue.y}");


they both give 0.

I require some way to extract the expanded width and height. How can I do that?

NOTE: The content of the child i.e. text is being assigned dynamically, also the parent/text is a prefab being instantiated dynamically i.e. via Instantiate, and this is not done in the start method.

Okay, here's more detail about the hierarchy and stuff:
The hierarchy

Parent:

Interface

MessageField

OutputSection

Viewport

ChatPanel

So, eventually chat bubbles are supposed to stack on top of one another, they get parented to ChatPanel, and each child is a prefab instantiated as mentioned above, the prefab detail are:

hierarchy

stackItem

ItemParent

Tag

TextItem (Note the MessageTextItem is basically not used, it's there for some old code reasons)

ChatTextBubble

These are expanded below:

When I send message, I log the width and height of the white part of the chat bubble which is basically the **chatTextBubble above, using the code: **

   var expRect = chatTextBubble.GetComponent<RectTransform>();
Debug.Log(\$"dvz:: expRect width is: {expRect.rect.width} and height is: {expRect.rect.height}");



and it logs 0. Please tell me what am I doing wrong. It looks similar to below example but, not working.

This is what it looks like during gameplay:

EDIT: I just noticed, the chatTextBubble has this during gameplay:

but, logs 0 when I log with the above mentioned code.

• Can you share a screenshot of your project hierarchy while the game is running and the child UI objects are spawned? Oct 28, 2021 at 16:29

Something is amiss with your setup, because RectTransform.rect will contain correct updated values as ContentSizeFitter manipulates its dimensions.

Here is a minimum verifiable example. Project hierarchy:

Code, attached to Test Controller GameObject:

using UnityEngine;
using UnityEngine.UI;

public class GetParentSize : MonoBehaviour
{

public RectTransform parent;
public GameObject childPrefab;
float timer = 0;

void Update()
{
timer += Time.deltaTime;
if(timer >= .5f)
{
var child = Instantiate(childPrefab);
child.transform.SetParent(parent.transform);

timer = 0;
}
readout.text = string.Format("Parent Width: {0} units",parent.rect.width.ToString());
}
}


Code instantiates a UI prefab every .5s and sets the parent of the instanced gameobject to the gameobject with the ContentSizeFitter and HorizontalLayout components. Every frame it updates the UI readout text with the width of the gameobject with the ContentSizeFitter.

This is how the parent gameobject is configured in the inspector:

This is how the Test Controller is wired up:

And here it is in action:

• Hello, thanks for the answer, sorry I replied so late, let me check this out. Nov 1, 2021 at 17:50
• If you still can't get it to work, share more details of your set up -- what you are trying to do should work... Nov 1, 2021 at 18:02
• I'll post the entire thing Nov 2, 2021 at 3:12
• I posted the entire hiearchy and all related content, could you please take a look? Nov 2, 2021 at 3:33
• @BumbleBee Thanks for adding all the extra information -- I'll have a closer look later today... Nov 2, 2021 at 15:21

I just had the same problem and I found the solution was to use ForceRebuildLayoutImmediate (2018 version):

LayoutRebuilder.ForceRebuildLayoutImmediate(go.GetComponent<RectTransform>());


I can then get the dimensions with: go.GetComponent<RectTransform>().sizeDelta.x (Width) or .y (Height)