What is UnityScript?
UnityScript is the third language supported natively bu Unity. Inspired by Boo and Javascript, UnityScript is a
looser language that takes concepts from Javascript to allow for fast development without worrying about some of
the overheads that C# require.
UnityScript is not Javascript. While the syntax is superficially similar, UnityScript is a fully class based language
while Javascript is prototype based. There are many other smaller differences in the syntax and semantics, but the
other important difference at this point is that UnityScript has access to all the standard C# class libraries and
integrates natively with the Mono platform (and Unity libraries). You can find out more about the major differences
between UnityScript and Javascript on the Unity3D wiki.
The UnityScript Basics for Noobs (pdf) is a great resource and will run through the basics of scripting using
UnityScript. We will not attempt to cover the same material in these translation notes, but will instead focus on the
differences between C# and UnityScript. You can also find a list of UnityScript Keywords on the Unity3D wiki
which will come in useful as we dive into the language.
Generally, UnitScript syntax will look very familiar after C#:
C#
UnityScript
void NextGuess () {
guess = (max + min) / 2;
print ("Higher or lower than " +
guess);
print ("Up = higher, down = lower,
return = equal");
}
function NextGuess() {
guess = (max + min) / 2;
print ("Higher or lower than " +
guess);
print ("Up = higher, down = lower,
return = equal");
}
Declaring Classes
Many of the things we have to specify in C# are optional in UnityScript. Most obvious is the class declaration. In
C# each file is a class, and the class has to be explicitly declared within that file. This can lead to problem when
renaming files but not the class inside, or vice versa. This can be omitted in UnityScript for classes that extend
MonoBehaviour:
C#
UnityScript
using UnityEngine;
using System.Collections;
The Code to define the behaviour of the class can be added straight into the file without explicitly declaring it. The
class name is then the script name by default. While this is useful, sometimes we need to create a class that inherits
from another type, or implements an interface. In that case, we can use explicit class declaration similarly to C#:
class MyType extends ParentType implements AnInterface{
// class Definition here ...
}
UnityScript
using UnityEngine.UI;
import UnityEngine.UI;
And since were accessing the same underlying libraries, we can use the same namespaces and well find the object
were used to from C#.
Declaring Methods
Method declaration is very similar to C#, but function return types do not need to be specified when declaring a
function. For example, we had to explicitly tell the compiler that our NextGuess() function returned nothing by
using the void keyword, but we dont have to do this in UnityScript. We do however have to use the function
keyword to declare functions:
C#
UnityScript
void NextGuess() {
guess = (max + min) / 2;
print ("Higher or lower than " +
guess);
print ("Up = higher, down = lower,
return = equal");
}
function NextGuess() {
guess = (max + min) / 2;
print ("Higher or lower than " +
guess);
print ("Up = higher, down = lower,
return = equal");
}
As we can see, the rest of the function is essentially the same. We can also optionally define a return typr to make
the code more efficient (no need to infer the type) and help us catch errors at compile time by defining the return
type after the function declaration using a colon:
function MyMethod() : ReturnType {
}
Input arguments can be defined as in C#, except that the colon is once again used to define type. This is optional
and can be omitted.
function MyOtherMethod(firstArg : TypeOfFirstArg, secondArg : TypeOfSecondArg){
//...
}
function YetAnotherMethod(arg1, arg2, arg3){
//...
}
Declaring Variables
Declaring variables in UnityScript is a little different to C#. We use the var keyword to declare a new variable and
the type of the variable is optionally defined after the variable name and separated from it by a colon:
var foobar; // An untyped variable called foobar
var max : int; // An integer variable named max
var myTransform : Transform; // A Transform variable named myTransform
Full translation
Taking all the above into consideration gives us the following for the full version of NumberWizard.js:
#pragma strict
var max : int;
var min : int;
var guess : int;
function Start() {
StartGame();
}
function StartGame() {
max = 1000;
min = 1;
guess = 500;
max = max + 1;
print ("========================");
print ("Welcome to Number Wizard");
print ("var number: Pick a in your head, but don't tell me!");
print ("The highest number you can pick is " + max);
print ("The lowest number you can pick it " + min);
print ("Is the number higher or lower than " + guess);
print ("Up = higher, down = lower, return = equal");
}
function Update() {
if (Input.GetKeyDown(KeyCode.UpArrow)) {
min = guess;
NextGuess();
} else if (Input.GetKeyDown(KeyCode.DownArrow)) {
max = guess;
NextGuess();
} else if (Input.GetKeyDown(KeyCode.Return)) {
print("I won!");
StartGame();
}
}
function NextGuess() {
guess = (max + min) / 2;
print ("Higher or lower than " + guess);
print ("Up = higher, down = lower, return = equal");
}
None of which should come as a surprise. Hopefully you can now translate simple scripts from C# into UnityScript