Best way to reach a component or script's function?

These ways

Function Call :

((GameMaster)GameObject.FindGameObjectWithTag ("GameManager").GetComponent (typeof(GameMaster))).RestartLevel();

Get Component:

((GameObject.Find ("waveCountdown").GetComponent<Text>())).text = Mathf.Floor(countDown).ToString ();

or Instead of these

Just define a variable Transform (For Function Call) or Text (Reach Component) and drag our gameobject or Canvas/Text Object to that variable in Hierarchy Window.

Which one is the best for performance?


GameObject.Find is the slowest. FindWithTag is faster. Inspector drag and drop is really fast. If you need it every frame (or very often) use the inspector. Otherwise if you can afford the search time it’s ok to use one of the find functions. If you have a unique tag for the object then it’s always a good idea to use FindWithTag instead of Find.