Game Development Reference
In-Depth Information
An even better way
Now, it has been pointed out (by those who like to test such things) that even the
GetComponent call isn't as fast as it should be because it uses C# generics to determine
what type of component you are asking for (it's a two-step process: first, you determine
the type and then get the component).
However, there is another overload of the GetComponent function in which instead of
using generics, you just need to supply the type (therefore removing the need to discover
it). To do this, we will simply use the following code instead of the preceding GetCom-
ponent<> :
myScriptRigidBody
=(Rigidbody2D)GetComponent(typeof(Rigidbody2D));
The code is slightly longer and arguably only gives you a marginal increase, but if you
need to use every byte of the processing power, it is worth keeping in mind.
Note
If you are using the " . " shortcut to access components, I recommend that you change that
practice now. In Unity 5 , they are being removed. There will, however, be a tool built in
the project's importer to upgrade any scripts you have using the shortcuts that are avail-
able for you. This is not a huge task, just something to be aware of; act now if you can!
Search WWH ::




Custom Search