I am using C#.
GetComponent("STRING") vs GetComponent<Obj>();
What's the main difference between the two?
I typically use this method like so:
GS = (GameSystem)GameObject.Find("Manager").GetComponent("GameSystem");
I am using C#.
GetComponent("STRING") vs GetComponent<Obj>();
What's the main difference between the two?
I typically use this method like so:
GS = (GameSystem)GameObject.Find("Manager").GetComponent("GameSystem");
If you are using only C# in your scripts, there is no functional difference. The thing is, comparing types is faster than comparing strings, so generic method GetComponent() is faster than GetComponent(String).
However, since javascript does not support generics, there is no way to fetch components created as a .js file using GetComponent().So, if you need to get components from JS files, use GetComponent(String), for example GetComponent("MyComponent").
EDIT: Jessy corrected me - it seems that using generics in Unity's JS is a possibility. Anyhow, I made a small test of performances. It seems that average call for generic GetComponent() is about 10 times faster than its non-generic type. (10 millions calls in 2.45 seconds in contrast to 25 seconds on non-generic method).
Also, there is another benefit of using generic method - readability. It does not require a cast operator.
Like I said, if you are using only C# - stick to GetComponent(). If you are mixing script languages I'd go for string version and call it as few times as possible.
On a side note, consider making a reference (private variable) to the Manager in your script and get the component only once (not each time you are using it) because GameObject.Find() is extremely slow to be called too often (especially not in *Update() functions).