- Home /
Overriding an initial value in a subclass?
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!
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 void Reset()
{
someValue = 10;
}
}
Excellent. This is the answer. Thanks. For anyone else checking out this question, the Reset() function is built in $$anonymous$$onoBehaviour function that is called "when adding the component the first time". So you can initialize the values here.
Swapped the answer because this seems to be exactly the functionality to enable this.
Looks like Reset maps pretty well to the "defaultproperties" block in Unrealscript (if anyone cares :P)
no need for public btw if it's a mono behaviour function, unless you want to be able to call it yourself :)
Reset() didn't work for me, perhaps cause the component was already in the hierachy. However Start() did work to set the default value for the subclass.
Oh no. Setting the value in Start means that whatever how the user set value in the inspector, this value will be overriden by the value you gave in start.
This is a far different behaviour.
That is NOT overriding which happens at compile time. Thats changing the value at runtime. Overriding is a type of C# notation.
Answer by Tolufoyeh · Feb 13, 2015 at 12:01 PM
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 {} }
I read somewhere that if you override a variable you can access the BaseClass variable by using the (BaseClass) cast.
I think its all in the $$anonymous$$SDN page.
((BaseClass)d1).Name = "$$anonymous$$ary";
Where BaseClass is the baseclass (never!) and d1 is the derived.
Answer by djmorrsee · Aug 19, 2011 at 05:56 PM
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).
Hi, thanks for the reply!
The problem though, is that if I just use inheritance as usual, the default value that shows up in the inspector for both scripts is 5. When I add the BaseClass component to an object, I want the default inspector value to be 5. When I add SubClass component to an object, I want the default inspector value to be 10.
$$anonymous$$y main reasons for wanting to do things this way is to be able to take advantage of polymorphism to some degree by being able to access someValue without knowing what subclass it is, but I also want the default values in the inspector to be different for each subclass to avoid user error (if I know that the initial value for a class should always be 10, why leave it up to the user to switch it to this value?).
And unfortunately, the "Start" method doesn't work because it will overwrite any changes that have been made in the inspector.
$$anonymous$$aybe I'm just misunderstanding you. In the inspector for the base class, you can leave it at 5. Then in the inspector for the subclass, you can change it from 5 to 10 (or whatever), and it will only apply that change to the object with the subclass script attached. Sorry if im not understanding your thought process.
I might be misunderstanding myself, but the problem I was having was that if I attached the subclass to a GameObject, the initial value shown in the inspector is 5, the value I set in the BaseClass.
Problem is, I would like to be able to set an initial value specifically for that subclass so that ins$$anonymous$$d of displaying 5 initially, it would display 10.
As far as I'm aware, subclasses will always initially use their parent's values, and you'll have to change it for each case that you need.
Your answer
![](https://koobas.hobune.stream/wayback/20220612184415im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
Question about Overriding 1 Answer
Multiple Cars not working 1 Answer
Quick Question About Overriding in C# 1 Answer
Distribute terrain in zones 3 Answers