Display text with unknown length?

What should I use to display a string with an unknown amount of characters to me? This is the code I wrote for the label.

GUI.Label (new Rect(920,20,100,20), ((int)energy).ToString());
GUI.Label (new Rect(920,60,100,25), ((int)generator).ToString() + "  J/s");

Problem is when the counter gets too big, instead of clipping through the label it gets cut off by its border. I would try GUI.Box but I don’t want to waste clicks on it. Maybe I should ditch uGUI for the label and use 3D Text? Or GUI Text? Or maybe I’m missing some property in onGUI? I’m so confused at this point, too many UI’s, and I can’t find one that doesn’t restrict the length of a string.

Edit: I also tried doing this with a Rect, but I can’t stick an int.ToString as a string param. I think I really will just ditch uGUI and get some 3D text going. Still, just out of curiosity I would like to know how exactly to make a label that automatically resizes itself relative to the content it displays.

Rect lbl = GUILayoutUtility.GetRect (new GUIContent("text"),GUIStyle.none);
GUI.Label (new Rect(920,20,lbl.width,lbl.height), ((int)energy).ToString());

This sort of works but you need to specify max characters. A lack of a simple toggle in the Inspector for this is baffling.

Sounds like you have some space reserved for the increased counter, so the simplest way is to just make a wider rect in the first place.

If for some reason this is not possible, or you want the exact width for the containing rect, you can also get the width of the string from its GUIStyle.

GUI.skin.label.CalcSize(new GUIContent( ((int)generator).ToString() + "  J/s" )).x;

I recommend caching the string to avoid creating new strings every frame, as the GUI system for Unity is already quite heavy.