- Home /
how to write classes
can someone please explain to me how to write classes in javascript. I want to be able to write some but cannot find tutorials on how to do in in javascript. Could someone please explain how and try to break it down for me or send me a helpful link, it would be very appreciated. Thanks
Answer by Seth-Bergman · May 01, 2012 at 07:58 AM
pretty simple:
class myClass
{
var whatever : int;
var other : GameObject;
//etc
};
//then to create an instance
var class1 : myClass = new myClass();
Once the script is attached to an object, you can view all of the data members in the inspector, you can set their values there, or initialize with the start function;
From : http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html
Implicit Class declarations
Unity's Javascript has a feature whereby if you do not declare any class body, it is automatically implemented for you. For example, a script which contains only variables and functions (and no explicit class declaration) is automatically implemented as:
// Your javascript is converted to (approximately) class TheScriptName extends $$anonymous$$onoBehaviour { ... your script here }
This means that any Javascript script that you write without an explicit class body, automatically inherits from $$anonymous$$onoBehaviour, and is therefore a component which can be attached to a GameObject (which is usually what you want!).
It's worth noticing that if you extends a class from $$anonymous$$onoBehaviour, you shouldn't declare a constructor, according to the doc, but use the usual awake and start functions.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to approach different game modes 2 Answers
Accessing scripts in the same game object 3 Answers
Too many bugs 0 Answers
Instantiate, Prefabs, Classes 1 Answer