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
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:
TextItem (Note the MessageTextItem is basically not used, it's there for some old code reasons)
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: