- Home /
Question by
supremegrandruler · Oct 24, 2014 at 01:47 AM ·
stringcomponenttypedifferent
What's the difference between GetComponent and GetComponent(StringName)?
Can the type be different from the string name? Can it be set manually?
Comment
Best Answer
Answer by Kiwasi · Oct 24, 2014 at 02:06 AM
The generic form of the method is faster. The generic form is also type safe (it will throw a compiler error if the type does not exist).
The string form is slower. It is not type safe (errors will occur at run time).
The general preference should always be to use the generic form. There are some cases where the flexibility of the string form is required. (Say you need to store a bunch of strings in a file).
Correct and succinct, while there are some time you may need to use the string version, you really need to stop first and think about any better way to code the problem.