C# addendum
As promised, the complete translated C# code for the Tic Tac Toe game follows. There were a few tricky spots in the code that required a bit of tap dancing:
When a function returns a value, the value type needs to be declared in the function signature before the function name. So this function declaration in Unity Javascript:
function CheckForWin(square:GameObject):boolean
is translated like this in C#:
private bool CheckForWin(GameObject square)
In an earlier chapter, we dodged the requirements of the waitForSeconds
method and used a different function entirely. Now that we've had more experience with return statements, we can discuss what's required.
In C#, this line:
yield WaitForSeconds(3);
becomes:
yield return new WaitForSeconds(3);
The WaitForSeconds
method returns a result of type IEnumerator
. Therefore, any C# function that uses WaitForSeconds
must return IEnumerator
. The ShowStalematePrompt
declaration, for example, which uses the WaitForSeconds
method, translates from...