Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by kromenak · Aug 19, 2011 at 04:48 PM · c#inheritanceoverride

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

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;
     }
 }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jwinn · Oct 04, 2012 at 05:21 AM 1
Share

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.

avatar image kromenak · Oct 05, 2012 at 04:19 AM 1
Share

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)

avatar image M0rph3v5 · Nov 20, 2015 at 08:49 AM 0
Share

no need for public btw if it's a mono behaviour function, unless you want to be able to call it yourself :)

avatar image nicmarxp · Dec 30, 2017 at 09:18 PM 0
Share

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.

avatar image AnKOu nicmarxp · Sep 01, 2019 at 10:32 AM 0
Share

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.

avatar image Ziplock9000 · Feb 27, 2019 at 08:03 AM 0
Share

That is NOT overriding which happens at compile time. Thats changing the value at runtime. Overriding is a type of C# notation.

avatar image
1

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 {} }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Feb 13, 2015 at 11:14 AM 0
Share

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.

avatar image
0

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).

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kromenak · Aug 19, 2011 at 06:18 PM 0
Share

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.

avatar image djmorrsee · Aug 19, 2011 at 07:16 PM 0
Share

$$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.

avatar image kromenak · Aug 19, 2011 at 08:12 PM 0
Share

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.

avatar image djmorrsee · Aug 20, 2011 at 03:25 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges