I have a prefab that has a text component, a content size fitter, and a layout element. I instantiate the prefab and then fill the text field with some text. Then, I need to get the height of this text field, so I can set the size correctly. Apparently the way to do this is with the preferredHeight field. However, this isn’t working.
If I have the LayoutElement’s preferredHeight checkbox checked, then this preferredHeight value never updates as I fill in more text. It just stays at whatever value I give it in the prefab.
If I have the preferredHeight checkbox unchecked, then this value always returns -1 when I access it in code.
The only way that I can seem to make this work is to check the preferredHeight checkbox in the editor, after I have filled up the textfield. Then, it correctly shows the height. However, I can’t find another way to “check” this box programatically, or have it recalculate.
How do I do this?