From Unify Community Wiki
Revision as of 21:29, 21 November 2018 by Isaiah Kelly (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

End of support for UnityScript

Starting with Unity 2018.2, support for UnityScript (Unity's version of JavaScript) has ended. C# is now the only supported language going forward. You can learn more about the reasons for this change here. There is an official UnityScript-to-C# Automatic Conversion tool to help those wanting to transition their UnityScript projects over to C#.


Chapter 1

JavaScript for Unity (aka UnityScript) is a customized version of the popular JavaScript language, built for the Unity platform. However, it resembles JScript.NET more than actual JavaScript/ECMAScript.

This chapter will provide a broad overview of the language, which later chapters will go into more detail to cover. Overall, this language specification is meant primarily for a user and will not be comprehensive enough for a language implementer.

Getting Started

It wouldn't be a language specification without having a "Hello, world" example. In Unity, you will need to create a new JavaScript asset in your project view and name it 'HelloWorld' (extension is hidden). Here it is:

function Start() 
    print( "Hello, world" );

When you attach this to a GameObject in Unity and hit Play it will print "Hello, world" once to the console.

To attach the script to a GameObject, select the following in the Unity menu:

 GameObject > Create Empty

This will create a new empty GameObject. You can then see the new GameObject in the Heirarchy view with the name: "GameObject".

Drag the script from the Assets view onto the newly created GameObject in the Heirarchy view. If you select the GameObject (in the Heirarchy view) you will then see that, in the Inspector view, the GameObject includes the script in its list of properties.

Click on the play arrow above the Game view. In the text field below the Game view you should then see "Hello, World".

The JavaScript source code you write for your Unity project is usually stored in one or more text files and must have a file extension of .js, as in HelloWorld.js. Unity does this for you if you create your scripts from within the editor. There is no command-line compiler for JavaScript, however, your scripts get compiled by the Unity editor as soon as you save them.

Examining our script further shows:

  • The Start method as a member of the implicit class HelloWorld. It has no static modifier, so it is an instance method instead of a class method.
  • The "Hello, world" output is produced using an inherited print() method from MonoBehaviour.


JavaScript for Unity has reference and value types. Value types are simple types such as char, int, float, enum, etc. Reference types include class types, arrays, and delegates.

Value and reference types are different in the way the data is associated with a variable. For value types, the variable has a one-to-one relationship with the data, which means that only one variable is associated with the data. For reference types, it is possible to have a many-to-one relationship with the data. Two reference type variables can point to the same instance of a class and affect that instance independently. Here is an example:

class SomeClass
    var someValue = 0;
function Start()
    print( "Value types" );
    var intValue = 1;
    var anotherIntValue = 2;
    print( intValue );
    print( anotherIntValue );
    anotherIntValue = intValue;
    intValue = 999;
    print( intValue );
    print( anotherIntValue );
    print( "Reference types" );
    var classVar = SomeClass();
    var anotherClassVar = classVar;
    print( classVar.someValue );
    print( anotherClassVar.someValue );
    classVar.someValue = 111;
    print( classVar.someValue );
    print( anotherClassVar.someValue ); 


Value types
Reference types

You will notice from the output that assigning intValue to anotherIntValue copies the data, whereas assigning classVar to anotherClassVar points the two variables to the same data.

Predefined Types

JavaScript for Unity has the following predefined types:

Type Description Example
sbyte 8-bit signed integer. var n : sbyte = -2;
short 16-bit signed integer. var n : short = -2;
int 32-bit signed integer. var n : int = 2;
long 64-bit signed integer. var n : long = 2;
byte 8-bit unsigned integer. var n : byte = 2;
ushort 16-bit unsigned integer. var n : ushort = 2;
uint 32-bit unsigned integer. var n : uint = 2;
ulong 64-bit unsigned integer. var n : ulong = 2;
float Single-precision floating point value. var f : float = 2.0;
double Double-precision floating point value. var f : double = 2.0;
boolean Represents a true / false value. var b : boolean = true;
char A unicode character. var c : char = "c"[0];
String Represents text as a series of Unicode characters. var s : String = "Hello, world";

Additional built-in types (without explicit importing) are listed in the Unity Script Reference

A predefined type is shorthand for a system-provided type (e.g. boolean is really System.Boolean). You should use the shorthand predefined types instead of the system-provided type when you have the option.


Predefined types can be converted between implicitly (e.g. sbyte to short). Explicit conversions are supported for reference types, but not for predefined value types. Be careful when converting between predefined types that have different min and max ranges. For example:

function Start() 
    var longValue : long = System.Int64.MaxValue;
    var intValue : int = longValue;

will throw an "OverflowException: Number overflow" at run-time because an integer type cannot hold the max value that a long integer can hold.

Array types

There are two array types in JavaScript for Unity: built-in arrays and the Array class. Array documentation from Unity Script Reference

Personal tools