- Home /
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.
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.
Okay to clarify. I want to create a class from which it creates a gameObject. I want that gameObject to show some variables in the inspector. Now, I know its possible to create a class which then can add another script component to a gameobject, but I dont want that because I want everything in one file. The aim is not to have two script files to achieve one purpose.(ie. a class and a script)
Answer by jashan · Aug 06, 2010 at 05:16 AM
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.
Answer by sanfelipe007 · Nov 02, 2012 at 07:59 AM
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.
Answer by Jackda12th · Dec 29, 2012 at 04:41 PM
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.
Your answer
Follow this Question
Related Questions
How do I show a Classes Vars in the inspector (Javascript)?? 2 Answers
Multiple classes inside each other 1 Answer
Is it possible to create a script dinamic like animation>size controling the Elements variables? 1 Answer
Change a Class in the inspector 1 Answer
Array of abstract class in inspector 2 Answers