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!
7 Comments
It's a welcomed introduction! But what's the real interest in switching to C#?
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.
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…
Thank you for your article,keep it up
You can actually use print in C#, too. I use it all the time. :)
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
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.
About col000r
Markus HoferIndie Game Developer
Austria, Europe
GAMES
NEW ORBITAge of Curling
Speedlap Red
RECENT POSTS
anyonemost peopleMERCH