First of all: It was a lot easier than I expected. And if you’re planning to switch as well, I hope I can make it even easier for you with this series of posts.
First I bought C# in a Nutshell, a very straight-forward book that teaches you almost everything you need to know in as few sentences as possible. Cheap and good – you really don’t need much else. It is, however, not Unity-specific and that is where I come in!
1) Variables and Types
var size : float = 0.5;
var keyword : String = "Skylight";
var isOn : boolean = true;
private var hiddenVar = 0; //in JS variables are shown in the inspector by default and you have to mark them as private to hide them
var visibleVar = 1;
float size = 0.5f; //you have to add an f to make it a float-value! (without the f it would be a double and wouldn't fit into a float variable)
string keyword = "Skylight"; //notice the lowercase s!
bool isOn = true; //notice: bool instead of boolean in JS!
int hiddenVar = 0; //in C# variables are private by default...
public int visibleVar = 1; //...and you have to mark them as "public" if you want to see them in the inspector or access them from another script
//Add the f even if you're not using a variable - if a function is expecting a float value, it won't take anything but a float!
2) Type Conversions (Casting)
You want to convert one type to another? Here’s how:
var f : float = 0.08; // create a float
var i : int = f; //assign it to an int
print(i); // Done! - Result: 0
float f = 0.08f; // create a float
int i = (int) f; // in c# you have to use explicit casts (the target type set in braces) if any information is in danger of getting lost during the cast
Debug.Log(i); //Result: 0
The other way around works without explicit cast though (as no information is in danger of getting lost)
int i = 5;
float f = i;
Debug.Log(f); //Result: 5