- Home /
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
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.
Answer by Ashkan_gc · Oct 14, 2011 at 07:31 AM
there are 3 types of scripts in unity as others said.
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.
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.
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
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!
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.
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
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.)
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.
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
![](https://koobas.hobune.stream/wayback/20220613010303im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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