BLACKISH DEV BLOG Support
 

 

Unity: Making the Switch from JS to C# (Part 2)

3.a) Functions

Basic functions are simple enough in Javascript, but might seem weird in C# at first. You just need remember this: [scope] [return type] [FunctionName] (varType varName).
JS
function WriteSomething (str : String) {
print(str);
}

C#

public void WriteSomething (string str) { //"public" means it can be accessed from everywhere (scope), "void" is the return type (void means it doesn't return a value)
Debug.Log(str);
}

3.b) Return

In Javascript you just do it, in C# you need to think ahead and put the return type in the declaration of the function…

JS

function ReturnSomething () {
return "Something";
}

C#

public string ReturnSomething () { // In C# you have to define the type of the value that will be returned - in this case it will return a "string"
return "Something";
}

3.c) Yielding

In Javascript yielding is easy… Just use yield. In C# you have to set the return type to IEnumerator if you want to yield inside a function. (This should also make you understand why you can’t yield and return inside one and the same function…)
JS
function Start () {
yield new WaitForSeconds(2.0);
yield DoIt();
print("Done!");
}

function DoIt() {
print("Doing it...");
yield new WaitForSeconds(0.5);
print("Almost done doing it...");
}

//Output:
//Doing it...
//Almost done doing it...
//Done!

C#

IEnumerator Start() { //if you want to yield inside start you have to change "void" to "IEnumerator"
yield return new WaitForSeconds(2f); //notice the f after the 2! - float
yield return StartCoroutine(DoIt()); //Note: without the "yield return" it would simply start the coroutine and continue, thus printing the "Done!" before the "Almost done..."
Debug.Log("Done!");
}

public IEnumerator DoIt() { // "public" isn't really needed in this example, but you need to use it if you want to call this function from outside this script!
Debug.Log("Doing it...");
yield return new WaitForSeconds(0.5f);
Debug.Log("Almost done doing it...");
}

//Output:
//Doing it...
//Almost done doing it...
//Done!
Bookmark the permalink. Follow any comments here with the RSS feed for this post. Trackbacks are closed, but you can post a comment.

7 Comments

    Mathieu on December 11, 2009 at 10:08 | Permalink

    It's a welcomed introduction! But what's the real interest in switching to C#?

    Aglet on December 11, 2009 at 13:18 | Permalink

    The IEnumerator part took me days to figure out. If only I'd had this!

    Keep up the awesome explanations, they're great reference material.

    col000r on December 11, 2009 at 15:50 | Permalink

    Mathieu: Well, my personal reasons are that weird stuff keeps happening in Javascript… I've been trying to come up with a somewhat more elaborate answer, but no luck. Let's say it's a gut-feeling that's driving me away from JS…

    Aglet: Thanks for the kind feedback! I'll post more as I run into things…

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

    Thank you for your article,keep it up

    torncanvas on January 7, 2010 at 03:37 | Permalink

    You can actually use print in C#, too. I use it all the time. :)

    Geoff on March 7, 2010 at 06:12 | Permalink

    Cheers for starting this article. I'm just starting with Unity though I've got tons of C# experience. Translating from JS examples to C# got me hung up a couple of times.

    One is assigning to a single x,y,or z variable on a position vector, such as:
    transform.position.x = 0.5f;
    Gives you the error:
    "Cannot modify the return value of 'UnityEngine.Transform.position' because it is not a variable"
    So you need to use something like this:
    Vector3 position = transform.position;
    position.x = 0.5f;
    transform.position = position;

    Which I found from this thread:
    http://forum.unity3d.com/viewtopic.php?t=6900&highlight=properties

    Geoff on March 7, 2010 at 07:15 | Permalink

    Also in the Yield section you might want to bring some attention to using StartCoroutine() to call a method that returns an IEnumerator;

    Such as to call your public IEnummerator DoIt() method from some random place in code you need
    StartCoroutine(DoIt()); specifically and not just
    DoIt():
    That hung me up a bit tonight.

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>