Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by gtzpower · Dec 30, 2011 at 11:45 PM · subclassing

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!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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;
     }
 }
Comment
Add comment · 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
1

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

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 Rod-Green · Dec 31, 2011 at 05:42 PM 0
Share

I new there was an issue with the constructor but I forgot what it was :P

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

6 People are following this question.

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

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


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