- Home /
Changing inspector default values from subclass OR hide from inspector in subclass only.
I want to subclass "Weapon" as a "Cannon" and set some default values in the Cannon class such as fire rate, damage, and so on. I want to be able to add a Cannon to a GameObject and have it use the Cannon's default values in the inspector, not the generic Weapon's values. Per this post, I understand that this is not possible...
http://answers.unity3d.com/questions/158319/overriding-an-initial-value-in-a-subclass.html
As a second best alternative, is there any way to hide those values in the inspector if it is a Cannon, but have them still accessible if it is a weapon.
I want to do this because if I am setting values in the Start() function of the Cannon (seems to be the best option), the values in the inspector are useless and only serve to confuse the developer. I still want the concrete Weapon class usable as is though for testing and playing.
Thanks!
Answer by Rod-Green · Dec 30, 2011 at 11:50 PM
Yes you can..
class BaseClass : MonoBehaviour
{
public int someValue = 5;
}
...
class SubClass : BaseClass
{
public SubClass()
{
someValue = 10;
}
}
Answer by Bunny83 · Dec 31, 2011 at 10:49 AM
You should use the Reset function for this. The constructor can be called multiple times (at each deserialization) and all serialized values get overwritten with the stored values.
class BaseClass : MonoBehaviour
{
public int someValue = 5;
}
class SubClass : BaseClass
{
public void Reset()
{
someValue = 10;
}
}
I new there was an issue with the constructor but I forgot what it was :P
Your answer
![](https://koobas.hobune.stream/wayback/20220613062525im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How is it best to distribute a complex unity project to other devs as asingle dll 1 Answer
PropertyDrawer for list. Subclass item fields not showing. Help needed! 2 Answers
how to C# subclassing a subclass of MonoBehaviour? 2 Answers
Using GetComponent with a subclass 3 Answers
How to have a generally usable class call an abstract method 0 Answers