• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Grimmy · Aug 05, 2010 at 01:38 PM · javascriptinspectorclass

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.

Comment

People who like this

0 Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 3dDude · Aug 05, 2010 at 01:39 PM 0
Share

what kind of vars do you mean?

avatar image Grimmy · Aug 05, 2010 at 01:41 PM 0
Share

ints, strings, floats...anything :)

avatar image skovacs1 · Aug 05, 2010 at 02:04 PM 0
Share

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

avatar image skovacs1 · Aug 05, 2010 at 02:08 PM 0
Share

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.

avatar image Grimmy · Aug 05, 2010 at 03:51 PM 0
Share

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)

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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 w$$anonymous$$ch implicitly will create the class with the name of the script file. And that class will automatically be a subclass of "MonoBehaviour" w$$anonymous$$ch 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, w$$anonymous$$ch in JavaScript/UnityScript is done by writing "class MyClass extends MonoBehaviour" ... w$$anonymous$$ch in the end is the same as not writing the classname explicitly at all.

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

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

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 t$$anonymous$$s...

 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 t$$anonymous$$s...

 variablename.classvar=anyvalue;

I t$$anonymous$$nk that's all you need. Good Luck.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

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.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

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

How do I show a Classes Vars in the inspector (Javascript)?? 2 Answers

Should I manually delete my custom class instances? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges