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
JS
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;
C#
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!
GiveMeAFloat(0.25f);
2) Type Conversions (Casting)
You want to convert one type to another? Here’s how:
JS
var f : float = 0.08; // create a float
var i : int = f; //assign it to an int
print(i); // Done! - Result: 0
C#
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
5 Comments
Great initiative!
Your C# cast is incorrect though. First off, simple types *should* have implicit casts, so myInt = myFloat should work just fine. Second – the cast works like this: myTransform = (Transform)myComponent (you have it reversed in your article).
Keep it up!
Thanks a lot for pointing that out – I corrected it in the post!
First of all. Thanks very much for your useful post.
I just came across your blog and wanted to drop you a note telling you how impressed I was with the
information you have posted here.
Please let me introduce you some info related to this post and I hope that it is useful for community.
There is a good C# resource site, Have alook
http://CSharpTalk.com
Thanks again
Rahul
Great initiative!
This is a nice article..
Its very easy to understand ..
And this article is using to learn something about it..
c#, dot.net, php tutorial
Thanks a lot..!
About col000r
Markus HoferIndie Game Developer
Austria, Europe
GAMES
NEW ORBITAge of Curling
Speedlap Red
RECENT POSTS
anyonemost peopleMERCH