O'Reilly logo

Unity 3 Game Development HOTSHOT by Jate Wittayabundit

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Class declarations

You can define classes in JavaScript, in a similar way as you do it in C#. The following example is a class that inherits from MonoBehaviour.

JavaScript:

class MyClass extends MonoBehaviour {
  var myVar = 1;
  function Start() {
    Debug.Log("Hello World!");
  }
}

C#:

class MyClass : MonoBehaviour {
  public int myVar = 1;
  void Start() {
    Debug.Log("Hello World!");
  }
}

However in JavaScript, if you're inheriting from MonoBehaviour, you don't need to write a class body at all. You can also write the following script in JavaScript, which will get a similar result as the preceding JavaScript:

var myVar = 1;
function Start() {
  Debug.Log("Hello World!");
}

Unity will automatically implement an explicit class body for you.

You can also write classes ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required