Question by
Oracmail · Sep 29, 2016 at 06:04 PM ·
getcomponentgameobject.findgameobject.findgameobject
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?
Illustration:
Comment
Best Answer
Answer by JedBeryll · Sep 30, 2016 at 05:44 AM
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.