why do I need to type variables classes?

Unity seems to work, wether I type float or boolean (for example) after class name or not.

Could you explain me why I need to type it?

When using Javascript in Unity, you don't have to explicitly declare types. Such as:

var myInt = 5;

It's smart enough to know that you are declaring an int in this case. There are times when it can't figure out at compile time, so instead it has to figure out and typecast your variables at runtime, which can be costly in performance.

If you put "#pragma strict" at the top of your scripts, an error will be thrown if the type cannot be determined at compile time. Use this if you are worried about that little bit of extra performance.

Assuming you mean variable names:

Javascript's compiler has the ability to (most of the time) implicitly assign the right type to the variable

In those cases, you don't need to specify it

On the other hand, sometimes it can't, and it's forced to try guess the type at runtime

This can lead to some spectacular runtime exceptions, and even when it works, it's slower than typing it yourself.

The best practice is to type anything that isn't explicitly defined by the function or variable you're assigning to it


var myInt = 0; //sets myInt to be an int

var myInt = FunctionThatReturnsAnIntExplicitly(); //sets myInt to be an int

generally you should never create objects like this:

var myInt;

It's asking for trouble