Welcome!

Think Labs is an ongoing effort by Seven2 to provide research and educational opportunities in the web development and mobile field. To see what we’ve been cookin’ up, check out our blog postings.

Created by
Seven2 Login

Categories
Tags
Team Think Labs | Baby steps from JS to C# in Unity
3337
single,single-post,postid-3337,single-format-standard,ajax_fade,page_not_loaded,,,wpb-js-composer js-comp-ver-4.2.3,vc_responsive

Baby steps from JS to C# in Unity

I recently decided to suck it up and start learning C# due to the better performance from using C# over JS, extra performance you can use for things us designers love, more sexy graphics and animation. For those of you who might be similar to me, (Designers who happen to program…) C# might have always sounded intimidating for those of us who consider ourselves ‘developers’ and not so much ‘programmers’. I’m here to tell you, it’s not that scary. I wanted to take some to time to point a few of the differences between Javascript and C# scripting for Unity Development that I’ve noticed. I’ll start with some of the simple stuff and work up from there.

 

Variables

One of the differences is the order in which you construct your variables. See the examples below.

Javascript:

public var waitTime:float = 1.0;

C#:

public float waitTime = 1.0F;

You’ll notice the main difference really is the just order in which you declare information. In C# you declare your variable ‘type’ first before the ‘name’. You’ll also notice in this example an “F” and the end of the float value. In C# a float must have this.

Another thing with C# not paying you any favors is that when declare in new instance to variable you have to say ‘new’ or things break:

 Javascript:

var hit:RaycastHit = RaycastHit();

C#:

RaycastHit hit = new RaycastHit();

 

Functions (Methods)

I won’t get into to much detail here as to keep things simple and I don’t think I have the programming knowledge to fully explain it. So for this one I’m going to do a bit of a cop out.

Javascript:

public function whatEver ()
{
//stuff
}

C#:

public void whatEver ()
{
//stuff
}

This example is pretty basic. I’m you sure you can make out the differences. You’ll notice the void before the Method name in C#, this is just the return type, which in this case nothing is being returned. There is definitely more to go over here, but like I said, keeping things simple for now…

 

Yields

This one actually took me a little bit to get a handle on… This is an example of wanting to print something to the console wait 5 seconds and print again.

Javascript:

print (Time.time);
yield WaitForSeconds (5);
print (Time.time);

C#:

print (Time.time);
StartCoroutine(theWaitTime(5.0F));

IEnumerator theWaitTime (float theWait)
{
yield return new WaitForSeconds(theWait);
print (Time.time);
}

Now I’m not a computer science major so I don’t really have the know how to explain this in full. But you’ll notice that unlike javascript, C# doesn’t really fill in the gaps for you the way JS does. When using a ‘yield’ in C# it needs to have it’s own Method with an IEnumerator datatype to handle the yield. You also notice you need to physically call the coroutine.

Okay my head hurts, so I think that’s all I would be able to cover for now. One way to convert over is to try and code things in JS and then go through bit by bit with the Unity Script Reference page open. Each example in the documentation has a drop down option to see examples in Javascript and C#, I would recommend switching between the two and learning the differences in them bit by bit.