if statement inside variables of a class

Hi all

I have made an inventory system but i would like to organize them some more.
Now im using a class with variables like damage, strenghtreq, healthadd, potion effects etc.
i also have a variable that declares the type of the item(it’s an enum) now i have it setup in my script that it only uses variables like ‘‘health add’’ when it has a type like ‘‘food’’.

my question:
how could.i make it that when i select e.g. ‘weapon’ that only then the variables in the inspector show up.

hope that my question is clear :slight_smile:

-Gijs

You will need to do some custom editor scripting for that.

What you want is a CustomEditor.

[Edit - Better explanation and example code]

Custom Editors are how you customize how the specified MonoBehaviour is inspected, you do this by using the CustomEditor attribute just before declaring the class, which binds that Editor class to your run-time MonoBehaviour, and then implementing the OnInspectorGUI method, which behaves basically identically to the OnGUI method of a MonoBehaviour.

One key difference between the OnGUI and OnInspectorGUI methods is that you have access to the EditorGUI and EditorGUILayout utility classes, as well as some others which are specific to Editor classes. For more info, just check the Scripting docs for Editor classes.

Here are some code examples, which I have tested and do work. Note that in order for any Editor class to work it must be in a folder called “Editor”. (Crazy, I know.)

Example run-time class: (JS)

#pragma strict
public class Foo extends MonoBehaviour
{
	var itemType : ItemType = ItemType.Sword;
	var strengthReq : int;
	var damage : int;
	var healthAdd : int;
	var manaAdd : int;
}

//our public enum
public enum ItemType{Sword,Food}

Example Editor class: (JS)

#pragma strict
@CustomEditor(Foo) //This binds this Editor to the MonoBehaviour of type Foo.
class FooEditor extends Editor
{
	function OnInspectorGUI()
	{
	    //we need to cast the target of this inspector as the class that it is to gain access to its members.
        var script : Foo = target as Foo; 
		
	    script.itemType = EditorGUILayout.EnumPopup(script.itemType);
 		
	    switch(script.itemType)
	    {
	        case ItemType.Sword:
	       		script.strengthReq = EditorGUILayout.IntField("Strength Req", script.strengthReq);
	       		script.damage = EditorGUILayout.IntField("Damage", script.damage);
	        break;
	        
	        case ItemType.Food:
	        	script.healthAdd = EditorGUILayout.IntField("Health Add", script.healthAdd);
	       		script.manaAdd = EditorGUILayout.IntField("Mana Add", script.manaAdd);
	        break;
	    }
	}
}

The idea here is that you define what itemType is first, and then use Unity’s immediate-mode GUI to your advantage by simply only drawing what you want to the Inspector, based on the value of itemType.

Hopefully that clears it up for you.

==