Publicly exposed Text component vs. GetComponent<Text>

Hey, was mostly wondering if there was any real benefit to using a publicly exposed Text element on the controller script versus using GetComponent since it’s attached to the same object.

class TextController : MonoBehaviour {
    private Text text;
    
    void Start() {
        text = GetComponent<Text>();
    }
    //... etc
}

Hi Nick,

GetComponent, specifically in this scenario isn’t really a performance issue. But where-ever possible try to find/get and then cache using a variable, rather than re-find/get.

One benefit of publicly exposing the field, again, in this scenario, is that your code could be used by someone else who is perhaps just doing the front end… if you were working on a project with a friend for example, maybe your have the code skills and they have the artistic flair… they can design a stunning scene and just drag the relevant object to your exposed field and boom - games running.

I am not personally a big fan of exposing variables unnecessarily, so considering the working environment can be worth while.