There are several variations of the GetComponent()
method, and it becomes prudent to call the fastest possible version of this method. The three overloads available are GetComponent(string)
, GetComponent<T>()
, and GetComponent(typeof(T))
. It turns out that the fastest version depends on which version of Unity we are running.
In Unity 4, the GetComponent(typeof(T))
method is the fastest of the available options by a reasonable margin. Let's prove this with some simple testing:
int numTests = 1000000; TestComponent test; using (new CustomTimer("GetComponent(string)", numTests)) { for (var i = 0; i < numTests; ++i) { test = (TestComponent)GetComponent("TestComponent"); } } using (new CustomTimer("GetComponent<ComponentName>", numTests)) { for (var i = 0; i < numTests; ++i) { test = GetComponent<TestComponent>(); } } using (new CustomTimer("GetComponent(typeof(ComponentName))", numTests)) { for (var i ...