- Home /
How do I show a variable in my base class in the inspector?
I have two classes, BaseClass -> DerivedClass;
public class BaseClass : MonoBehaviour { public Enum baseVariable; }
public class DerivedClass : BaseClass { public Enum derivedVariable; }
derivedVariable is visible in the inspector, but baseVariable is not. How do I make it visible?
Edit: It actually works fine with ints. I was just trying to simplify my code for the question. In my actual code, I use Enums, and I'm guessing it can't display the Enums because they are declared in the derived class.
Try changing the "P" in public to be lower-case at the beginning of the line BaseClass.
Oh, that was just a typo. This is just example code. In practice, should it work?
I'm not sure this will fit what you're trying to do, but it should access the baseVariable from the BaseClass script, and return it as a public variable in your DerivedClass script. I'm sorry if this doesn't help, I'm kind of out of my element with the class system.
public class BaseClass : $$anonymous$$onoBehaviour
{
public int baseVariable;
}
public class DerivedClass : BaseClass
{
public int derivedVariable;
public new int returnedVariable;
{
get { return base.baseVariable; }
set { base.baseVariable = value; }
}
}
Answer by Benoit Dufresne · Jan 20, 2014 at 12:25 AM
Your code works, just with an error: MonoBehaviour, not MonoBehavior.
Variables don't show up correctly in editor until the code compiles without any errors. Eliminate errors and all public variables will show up, no matter how many levels of inheritance.
Thanks, please accept that answer! Working on that karma y'know
Sorry, that was a typo, too. It's compiling without errors, but the variable still does not show up
Ah okay, so the code actually does work. The reason it wasn't working for me was that I was using Enums that were declared in derived class.
Answer by sirconnorstack · Jan 20, 2014 at 01:54 AM
It looks like Unity can't display a generic Enum variable. It has to know the type of the enum.
For example, if I have an enum State
public enum State { Idle, Walking, Jumping }
Then this will be displayed in the inspector:
public State currentState;
But this won't:
public Enum currentState;
Answer by lilsac · Nov 11, 2016 at 08:34 AM
Put [system.serializable] above the derived class definition
Your answer
Follow this Question
Related Questions
HideInInspector with inherited variables 1 Answer
Parent Class variables in Child Class's Inspector (C#) 0 Answers
An OS design issue: File types associated with their appropriate programs 1 Answer
Unity Editor - Class variable value contrain 4 Answers
Execute method from unknown script set as variable in inspector 1 Answer