Here are two versions of GetComponent in Unityscript
Here’s one…
private var brain:BrainScript;
brain = head.GetComponent(BrainScript);
And here’s the other, the Generic …
private var brain:BrainScript;
brain = head.GetComponent.<BrainScript>();
Does anyone know: is one faster than the other?
(Note … you can also I believe use GetComponent by passing it a string. So, GetComponent(“BrainScript”) would also work. If you do that, it is very slow. Generally one would never do that. I’m not asking about that. I’m asking about the two variations above where you pass in the type.)
Does anyone know if there’s any speed difference, or indeed any difference at all, between the two?
Cheers!
In C# generics do have a performance benefit.
– yusolaifdferthat's good to know. I'm wondering about Unityscript here
– FattieI don't know - but I think that they are exactly the same. GetComponent(BLAH) is effectively using the generic I believe but it avoids the beginner unfriendly syntax.
– whydoidoit@yusolaifdfer: the generic version of GetComponent has a performance penalty, both in C# and Unityscript. Furthermore, a generic List is slower than a built-in array of the same type, by quite a lot in the case of primitive types. Generics are only faster when compared to something like ArrayList, which requires boxing/unboxing. @whydoidoit:
– Eric5h5GetComponent(Blah)is effectively doingGetComponent(Blah) as Blah, not the generic version, which as demonstrated by GerryM (and mentioned by myself on many occasions ;) ) is a little slower.