Getting the Size of a TextMeshProUGUI object

I’ve seen a couple of threads here about getting the size of a TextMeshPro object using Object.bounds. ( TextMeshProUGUI get the width (prefersize) of the rendered text ) I’ve seen a post that says if you need the size of the object immediately, you need to use theForceMeshUpdate()Method. I’m trying to build and size a dynamic popup window, so I’ve attempted the following, but the Object.bounds.size.x is always 0. I’m sure I’m doing something really dumb but at the moment, I’m completely unsure what that might be.

foreach ( string val in values ) {
GameObject row = Instantiate(_cellInfoRowPrefab);

TextMeshProUGUI labelField = row.GetComponentsInChildren()[0];
TextMeshProUGUI valueField = row.GetComponentsInChildren()[1];

if ( firstRow ) {
labelField.text = label.Length > 0 ? $“{label} :” : “”;
firstRow = false;
}
else {
labelField.text = “”;
}

labelField.ForceMeshUpdate();

valueField.text = val.Trim();
valueField.ForceMeshUpdate();

Util.DebugMessage($“valueField: {valueField.bounds.size.x} labelField: {labelField.bounds.size.x}”);

if ( labelField.bounds.size.x > _maxLabelFieldWidth ) _maxLabelFieldWidth = labelField.bounds.size.x;
if ( valueField.bounds.size.x > _maxValueFieldWidth ) _maxValueFieldWidth = valueField.bounds.size.x;

_infoRows.Add(row);
}

Does labelField.text get set to the proper content?

Check the content of the labelField.textInfo before and after ForceMeshUpdate() to see if the character count appears to be correct.

When you do this, is the object or canvas enabled?

You can also use labelField.preferredWidth after calling the ForceMeshUpdate() or even use the GetPreferredValues() functions.

The text is being set correctly as it pops up completely unformated on screen.

The Canvas is disabled until I’ve built the menu, then I enable it.

I’ll check the preferredWidth and GetPreferredValues.

@Stephan_B , Thank you very much for your help. using preferredWidth worked perfectly.

When the Canvas is disabled, TMP doesn’t update the text object since it can’t be rendered. This is why I hinted at the preferredValues.