Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Memor-X · Oct 14, 2011 at 03:39 AM · javascriptinspector

Having Variables store a script in the inspector

i've been trying to make a script, this is it

 class WeaponList 
 {
     var point_weapon    : Weapon;
     var flag_obtained    : boolean;
     var main_ammo        : Ammo;
     var list_ammo        : Ammo[];
 }
 
 var list_weapons        : WeaponList[];

what i want this script to do in the inspector is allow the user to attach a script at Weapon and Ammo, however at the moment when i try and drag a script over to one of these the script doesn't attach, and i missing some code in this?

EDIT: there actully are scripts called Weapon and Ammo created, i use them as templates

Comment
Add comment · Show 1
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 syclamoth · Oct 14, 2011 at 06:41 AM 0
Share

You need to be dragging instances of scripts onto these- either objects in your scene which have been set up, or prefabs. You can't just drag the script from the project view and expect it to instantiate a new object for you there!

Is your intention to have smaller drop-downs in the inspector from which you can modify values? As long as they don't need to affect other objects in the scene (at least directly), you could use [System.Serializable] at the top of base classes, so that the inspector knows how to view them.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Ashkan_gc · Oct 14, 2011 at 07:31 AM

there are 3 types of scripts in unity as others said.

  1. MonoBehaviours are classes which you define in js without even using the class keyword and you can attach them using a gameObject/Prefab with that script attached. they are a component and can not be attached.

  2. normal classes (i.e derrived from System.Object) which if you want them to be shown in inspector to be able to change their values like Vector3's inspector, in C# you need to set [System.Serializable] attribute for them but in js it's implicit too just derrive from System.Object like the code example below.

  3. scriptableObjects are a great type which developers don't use that much. if you derrive from it then you can save the class as an asset in your project and you can drag assets of this type to a variable of the same type just like Texture2D and other asset types. to create an asset you need to use the UnityEditor.AssetDatabase class.

code example for the second type of scripts

 class test extends System.Object
 {
 //class definition
 }


if you want to know more about scriptableObject data scripts you can take a look at Character customization example project

Comment
Add comment · Show 1 · 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 jahroy · Oct 14, 2011 at 07:37 AM 1
Share

ScriptableObjects are awesome. Like you said, for some reason many people don't know about them. I didn't know about them until recently... I'm super stoked the folks from Schell Games talked about them in their presentation at Unite!

avatar image
0

Answer by jahroy · Oct 14, 2011 at 05:19 AM

What kind of objects are Weapon and Ammo?

Are they MonoBehaviors, simple classes, ScriptableObjects, or something else?

It makes a difference.

If they're simple classes you can declare them as public (the default) and they will appear in the inspector.

If they're MonoBehaviours, you can't directly drag and drop them onto another script.

In that case you would have to drag a GameObject that has a Weapon or Ammo script attached to it onto the Weapon and Ammo slots.

Comment
Add comment · Show 4 · 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 Memor-X · Oct 14, 2011 at 06:26 AM 0
Share

Weapon and Ammo are just .js files with variables in them, i do plan to put a function in Ammo but that's for much later, i would use Structs but as far as i know, Java doesn't use them

avatar image syclamoth · Oct 14, 2011 at 06:42 AM 0
Share

Well, then just use C#- it has all those useful features, and runs faster than javascript anyway! (also, please don't call javascript java they're really not the same thing.)

avatar image jahroy · Oct 14, 2011 at 06:50 AM 0
Share

I think you might get what you want if you put the variables inside classes named Ammo and Weapon.

Something like this:

  class Weapon
  {
     var name    :  String;
     van power   :  float  =  2.45;
  }

If you do that, you'll be able to edit them in the inspector without changing the code you posted.

If you just define variables in a .js file it will be treated as a $$anonymous$$onoBehaviour, which must be added to GameObjects.

avatar image jahroy · Oct 14, 2011 at 07:31 AM 0
Share

You can definitely use structs with Unity's version of javascript (aka UnityScript). Both Rect and Vector3 are examples of structs. That being said, I don't know if you can declare your own.

You can definitely add functions to classes:

 class Weapon
 {
     var name   :  String;
     var power  :  float;
 
     function shoot ()
     {
         print("Gun named " + name + " shooting with power " + power);
     }
 }

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Execute String as Code Line? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Object reference not set despite assigning instance of an object in GUI 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