- Home /
How to make subclass variables appear in inspector with enum
I have a script called item that is a constructor and has an enum that allows you to choose the item type. I also have another script inheriting from Item called Weapon that has specific variables. Is there any way that I could make the variables for that class show up based on what you choose from the enum? Thank you beforehand for any help. Here are my scripts:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Item
{
public int itemID;
public string itemName;
public string itemDescription;
public Sprite itemIcon;
public ItemType itemType;
public GameObject itemModel;
public int itemValue;
public int itemDurability;
public enum ItemType
{
Weapon,
Spell,
Consumable,
QuestItem,
WeaponPart,
Head,
Torso,
Hands,
Legs,
Feet,
None
}
}
And then there is Weapon which inherits from Item:
using UnityEngine;
using System.Collections;
public class Weapon : Item
{
public int weaponDamage;
}
It's very simple for testing purposes.
You need an editor script. http://answers.unity3d.com/questions/192895/hideshow-properties-dynamically-in-inspector.html
There are few others on the topic.
Answer by YoungDeveloper · Aug 21, 2015 at 10:48 AM
You wont be able to attach Weapon script to gameobject at all, as Weapon nor Item inherits from MonoBehaviour. Otherwise you have to write editor script. Here's and example i did and it works fine.
[System.Serializable]
public class Item{
public int itemID;
public string itemName;
public string itemDescription;
public Sprite itemIcon;
public ItemType itemType;
public GameObject itemModel;
public int itemValue;
public int itemDurability;
public enum ItemType {
Weapon,
Spell,
Consumable,
QuestItem,
WeaponPart,
Head,
Torso,
Hands,
Legs,
Feet,
None
}
}
[System.Serializable]
public class Weapon : Item {
public int weaponDamage;
}
public class Test : MonoBehaviour {
public Weapon weapon;
}
Your answer
Follow this Question
Related Questions
inheritance ? (how to do it in javascript) 1 Answer
Can't access an enum. I get an error. 1 Answer
Component Class Hierarchy 2 Answers
Unity 4.6b20 - Inheritance not working? 1 Answer
C# Inheritance Question 2 Answers