Game Development Reference
In-Depth Information
Class declarations
You can define classes in JavaScript in a way that is similar to C#. The following example
is a class named MyClass that inherits from the MonoBehaviour class:
// JavaScript user:
class MyClass extends MonoBehaviour {
var myVar = 1;
function Start() {
Debug.Log("Hello World!");
}
}
// C# user:
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 following the script in JavaScript, which will
fetch a similar result as the preceding JavaScript example:
var myVar = 1;
function Start() {
Debug.Log("Hello World!");
}
Unity will automatically implement an explicit class body for you. You can also write
classes that do not inherit from anything; however, you cannot place these scripts in the
game objects, but you have to instantiate them with the new keyword as follows:
Search WWH ::




Custom Search