I’ve got a class like this:
class BaseClass : Monobehavior
{
public int someValue = 5;
}
And so when I attach this to a GameObject, the default value displayed is 5.
Then, let’s say I have a subclass where I would like the default value of this variable to be some value other than 5.
class SubClass : BaseClass
{
//how do you do this???
public override int someValue = 10;
}
The above does not work, but is there a way to get this sort of behavior? My particular case is that each of several subclasses share a “speed” variable, but they each require this value to be different by default.
Thanks!
Yes you can…
class BaseClass : MonoBehaviour
{
public int someValue = 5;
}
…
class SubClass : BaseClass
{
public void Reset()
{
someValue = 10;
}
}
I came here because while I knew how to override in inheriting classes, I couldn’t find a way to access the overridden value in the inspector. I just realized while reading the comments that all I had to do was not override the variable in the inheriting class. AS long as I don’t do this, it shows up in the inspector.
My former code:
Base class:
public abstract int ScoreValue { get; set; }
Inheriting Class:
public override int ScoreValue
{
get
{return 15;} //the value you want to set
set
{}
}
Subclasses have full inheritance, so you wouldnt need to declare it again. You can either assign it in a Start method as you would any other variable, or the easier way is to set its value in the inspector.
(Note that private variables are not inherited).