- Home /
Question is off-topic or not relevant
Code Mistake in documentation
In the scripting documentation, section "Overview: Performance Optimization",
The C# code under: "Here foo will be dynamically typed, thus calling the function DoSomething takes longer than necessary - because the type of foo is unknown, it has to figure out whether it supports DoSomething function, and if it does, invoke that function."
MyScript foo = GetComponent();
should be:
MyScript foo = GetComponent() as MyScript;
The whole point of this example is to show that that line is bad code. I don't see your point.
I think they are trying to $$anonymous$$ch you about what is a often no-no. The first line is dynamic which is slower than the average grand mom, but the seconds is(yes) much quicker.
This site is for questions. If you find a documentation bug, use the bug reporter app. In any case, the page is quite wrong. For one thing, the text does not apply to C#, which doesn't have dynamic typing. The only reason C# code is there is because most of the code examples are written in Unityscript, then translated to the other languages with an automated tool. For another thing, the "dynamic typing" code isn't actually dynamically typed in Unityscript, as of Unity 3.4. As long as you don't use strings, GetComponent returns the type rather than Component. (Note that C# doesn't have this ability. You have to use generics or cast it manually.)