How do I create a JavaScript class that creates an object with viewable variables in the inspector?

Ie..a class that creates a cube and then I can tweak that cubes's vars in the inspector.

what kind of vars do you mean?

I think 3dDude, like myself, is confused by the ambiguity of your question. You want a javascript function within a class which instantiates an object containing "variables" that you can tweak in the inspector, but what do you mean by variables in this case? If you mean things like scale and position and rotation (variables accessible on the instantiated gameobject), then they are already tweakable. If you mean some variables of your creation that you store with the object, then you will have to also instantiate a script containing the variables and attach it to the gameobject

Also, is this not the same question as http://answers.unity3d.com/questions/17297/how-do-i-show-a-classes-vars-in-the-inspector-javascript. Please don't ask the same question twice. If your question was too vague/incorrect to get the answers you are looking for, please consider rephrasing for clarity.

So...let's say you are creating a pathing AI system, built up of path nodes that the character travels along. Do you want to be able to not only display a custom inspector display for the path itself, but also to be able to display custom inspector options for a node in the path, ifyou select it?

3 Answers

3

In JavaScript, you'd usually don't explicitly "create a class" but simply write the script which implicitly will create the class with the name of the script file. And that class will automatically be a subclass of "MonoBehaviour" which is the prerequisite for you to be able to attach it to a game object. And that's what you need to do to be able to change it's public member variables in the editor. In Debug mode, you'll also see private member variables.

In JavaScript, and variable that you declare outside of a method ("function") and that doesn't have the "private" modifier will be a public member variable.

So that's actually quite easy. If you have "class MyClass", you just need to make sure that you do extend MonoBehaviour, which in JavaScript/UnityScript is done by writing "class MyClass extends MonoBehaviour" ... which in the end is the same as not writing the classname explicitly at all.

This can be a bit confusing ... which is why in general I recommend using C#. Things are much clearer there. C# in Unity doesn't support the "just write stuff and it'll work and implicitly do everything right"-approach that's kind of common with UnityScript ... but the advantage is: When you read a C#-file, everything you need to know is written down there explicitly, so it's much easier to understand. Especially if you're new to programming.

Let’s say i want to make a class it would be…

    class NAME {
    }

Ok, it’s created, but the inspector do not detect it.
Because it need another variable to be represented, so it must be like this…

class NAME {
}
var variablename : NAME = NAME();

After that it will appear on the inspector.
even you can put the same variables on every class.
Personally i use it like it was an array.
You can use the variables like this…

variablename.classvar=anyvalue;

I think that’s all you need. Good Luck.

Make the cube with all the vars tweaked then save it as a prefab. When you change the prefabs vars it changes all the instantiated cubes in the game.