Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Disastercake on July 13, 2012, 03:14:33 PM

Title: Is there a buiilt-in way to avoid GetComponent?
Post by: Disastercake on July 13, 2012, 03:14:33 PM
I want to try to avoid GetComponent as much as possible in my code.  What is the best way to grab something like a UILabel component to change it's text when it's created at runtime?
Title: Re: Is there a buiilt-in way to avoid GetComponent?
Post by: JRoch on July 13, 2012, 06:23:43 PM
Create a local (private) variable of the type of the Component and only GetComponent<type> once for Awake() or Start().  From that point on you may reference the variable rather than having to look up the Component all the time.

I'm unsure whether GetComponent has a performance hit like Find does.
Title: Re: Is there a buiilt-in way to avoid GetComponent?
Post by: ArenMook on July 13, 2012, 07:44:49 PM
GetComponent isn't very expensive, but it's still better to cache the result.