Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Hiding Properties in Inherited Classes

Discussion in 'Scripting' started by jldooley1, Mar 25, 2021.

  1. jldooley1

    jldooley1

    Joined:
    Feb 15, 2020
    Posts:
    28
    I've created a class to deal with a new hit detection use case which inherits from an abstract class handling the main hit detection logic.
    The new class displays properties in the inspector which are redundant for its use case, and I'm wondering is there an easy way to hide them (or if it's a job for an InspectorGUI script)?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    You'd have to use a custom editor I guess.

    Frankly it sounds like you're kludging something into an inheritance pattern that doesn't really fit.
     
    Vryken and lordofduct like this.
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,375
    inheritance is likely not what you want here, like PraetorBlue suggested, and you'll have to write a custom editor

    thing is... having an editor hide the property doesn't mean it's not there anymore. It just means the inspector doesn't show it.

    if you shared your code we may be able to point you in a better direction of how to accomplish what you want
     
  4. jldooley1

    jldooley1

    Joined:
    Feb 15, 2020
    Posts:
    28
    Good to know, thanks. I'm ok with the property still existing, just wont do anything in the new derivation so I want to hide it away so that it's not mistaken for a necessary property. If it's a job for an editor script I'll probably grin and bear it until I can have a proper look at it.

    The stripped down version class and its derived classes are below.

    Code (CSharp):
    1. public abstract class Target : MonoBehaviour
    2. {
    3.     public TargetType targetType;
    4.  
    5.     //General hit detection and health logic
    6. }
    7.  
    8. public TargetAI : Target
    9. {
    10.     //Extended functionality for AI Behaviour, uses targetType
    11. }
    12.  
    13. public TargetPlayer : Target
    14. {
    15.     //Extended functionality for Player, uses targetType
    16. }
    17.  
    18. public TargetTrigger : Target
    19. {
    20.     //NEW
    21.     //Extended functionality for in-game triggers (shoot to interact), targetType not needed
    22. }
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,375
    Yeah, I'd just go a basic component approach. Unity is designed with a composited component based system first rather than inheritance first.

    Think of it how Rigidbody is for simulating physics, but it does not inherit from Collider.

    The so-called "is/has" situation. A Rigidbody HAS a collider, not is a collider. So you attach a Rigidbody and a Collider to the GameObject.

    AIBehaviour HAS a Target, not is a Target.

    Also, KISS, keep it simple stupid. Why is Target both offering up target information, resolving hit detection, AND health logic? Why not have a Health component for the health?

    Your code doesn't have enough information for me to really glean what it is you're doing (you've stripped pretty much anything of informative value). But I bet you could break apart all of this functionality into appropriate scripts that have the fields necessary only to themselves. If you showed us the actual code, we could likely show you what we mean.
     
    Kurt-Dekker, jldooley1 and mopthrow like this.
  6. jldooley1

    jldooley1

    Joined:
    Feb 15, 2020
    Posts:
    28
    There's probably a good few ways to improve the structure of the code but I wont have an opportunity to review it any time soon so I wont go dumping the various scripts on you.

    It isn't clear from the code above but Target isn't handling everything, it's more of a point of access for damage interaction (for instance, AI behaviour is a separate component decoupled from the TargetAI. TargetAI just holds additional code for triggering certain behaviors such as aggro). The idea with the inheritance pattern is that a gun or other damage source can use GetComponentOfType(Target) and polymorphism ensures it doesn't care what type of Target it is.

    Thanks for the advice, I appreciate you guys taking the time to give the feedback.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    And building upon lordofduct's high shoulders, you can even use interfaces in Unity to generalize access to components... this lets you make more abstract things such as IDamageable, IHealable, ITargetable, IShooter, etc.

    That can further decouple your scripts from each other.

    More blurbery I have scribbled in the past:

    Using Interfaces in Unity3D:

    https://forum.unity.com/threads/how...rereceiver-error-message.920801/#post-6028457

    https://forum.unity.com/threads/manager-classes.965609/#post-6286820

    Check Youtube for other tutorials about interfaces and working in Unity3D. It's a pretty powerful combination.
     
    jldooley1 and lordofduct like this.
  8. jldooley1

    jldooley1

    Joined:
    Feb 15, 2020
    Posts:
    28
    Thanks for all of this, it's been a great help.
     
    Kurt-Dekker likes this.