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
0
Question by ronronmx · Jun 15, 2012 at 07:21 AM · editorvariableinspector

Hide public field on base class but show it on derived class

Hey guys, quick question... I have my base class setup this way:

 using UnityEngine;
 using System.Collections;
 
 public class PowerUp : MonoBehaviour {
     
     public bool displayIcon = true;
     public Texture2D icon;
     public Vector2 iconPosition = Vector3.zero;
     public int uses = 5;
     public float passiveLifetime = 20;
    
     
     [HideInInspector] public bool isActive = false;
     [HideInInspector] public bool isPassive = false;
     [HideInInspector] public float dieAt = 0;
 
     // Functions and more below....
 
 }

Now I have my derived class:

 using UnityEngine;
 using System.Collections;
  
 public class StickyWheels : PowerUp {
 
     public Texture2D stickyWheelsIcon;
     
     void Start() {
 
         // Init stuff...
     }
 }

All the public fields show up in the inspector on both classes. But I really don't need them to be displayed on the base class, since they are different based on the object the scripts are attached too.

Is it possible to force the public fields to be hidden on the base script, and only have them visible on the derived class?

Thanks for your time guys! Stephane

Comment
Add comment · Show 25
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 asafsitner · Jun 15, 2012 at 07:25 AM 4
Share

Why would you want that?

If you have an object that uses the base class you'll want them to be shown so you could tweak them, and if you don't use the base class (the likely scenario for this sort of setup) then they shouldn't be hidden at all.

Either way, I don't see a reason to hide those fields on the base class in the first place.

avatar image Jessy · Jun 15, 2012 at 07:46 AM 1
Share

Public fields should never be used.

avatar image Wolfram · Jun 15, 2012 at 09:53 AM 1
Share

You can also make your base class abstract. This way you cannot drag the script PowerUp onto an object, only the derived versions, which is most likely what you are interested in.

avatar image whydoidoit · Jun 15, 2012 at 01:17 PM 2
Share

Well my properties don't show up in the inspector - just variables. There is this script which can be used to show them.

avatar image Wolfram · Jun 16, 2012 at 07:52 PM 2
Share

Umh, all the functionality contained in the base class is also available/present in all derived classes, that's the whole point of class hierarchies. For the same reason you can see the public fields of your base class in your derived classes. So it doesn't make sense to attach another base class object somewhere in your scene, since every Component that's derived from the base class will already "include" such a base class object, although it's not visible as an independent Component in the inspector.

If you do have some global parameters/settings/things that are identical for all objects of type "PowerUp" (which per definition includes all derived objects as well), you might want to consider making these static ins$$anonymous$$d.

Show more comments

1 Reply

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

Answer by Wolfram · Jun 17, 2012 at 01:06 AM

As a result of the rather long discussion in the comments, it turns out there wasn't actually a need to do precisely what was asked in the question. After clearing up some misunderstandings concerning class derivation, it turns out the problem was related to an unnecessary base class instance somewhere in the scene, and the fact that the base class initializations of derived instances never happened.

So the solution was for each base class to call their associated base.Start() as described below (my original comment copied from the question):

For your information, the "correct" way to do it using object-oriented programming is, you can keep the Start() in your base class. But declare it like this:

 protected virtual void Start()

Then, in your derived classes you use this instead:

 protected override void Start()

So if the object initializes, the Start() of the "most derived class" will be executed. But not the one from the base class - which probably caused your problem. You need to explicitly call it. So in your derived classes, you would write:

 protected override void Start() {
     base.Start(); // initialize base class

     // your regular Start() code here
 }

Note you'll need to do this in every derived class, and it also works if you have multiple derivations, ensuring that all Start()'s of all ancestors are called.

See any OOP tutorials on the web for more info.

NOTE: as @asafsitner pointed out, it isn't actually mandatory to provide the protected virtual/override modifiers, so you can just use your regular void Start() syntax. The important thing is that each derived class needs to call base.Start() in its Start() call (and similarly for each method of the base class that is overloaded this way, e.g. call base.Update() if both base and derived class have an Update() function, and if both need to be executed.

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

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

Edit child in parent list [Custom Inspector] 1 Answer

Variable in Inspector 1 Answer

customization of variables in script for in Editor? 2 Answers

Unity Editor - Class variable value contrain 4 Answers

how to give title for group of variables? 1 Answer


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