BLACKISH DEV BLOG Support
 

 

Unity: Making the Switch from Javascript to C#

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
Bookmark the permalink. Follow any comments here with the RSS feed for this post. Trackbacks are closed, but you can post a comment.

5 Comments

    AngryAnt on December 11, 2009 at 09:52 | Permalink

    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!

    col000r on December 11, 2009 at 14:35 | Permalink

    Thanks a lot for pointing that out – I corrected it in the post!

    rahul on December 23, 2009 at 14:11 | Permalink

    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

    hk網頁設計 on December 30, 2009 at 10:34 | Permalink

    Great initiative!

    Muhammad Azeem on March 23, 2011 at 11:44 | Permalink

    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..!

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>