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










