General Question On How C# Classes Correspond To Objects In Unity

I’ve done a couple of tutorials about C# and I think I get the general picture of how classes, inheritance and so on are supposed to work.
From what I learned so far about C#, I went in thinking I’d first create a class called “Person” and from this class, “Player” and “NPC” can inherit. Then “Enemy” can inherit from NPC and so on.
If I wanted to check if an object that came close to a door was a person and not an animal (because animals can’t open doors let’s say), I’d check wether the instance belonged to class Person. Since Enemies inherit from NPC which inherit from Person, the check should return true.

However in Unity, with each script I also create a new class of the same name. Then when I want to distinguish between things, there are separate sorting systems in place such as tags and layers and components I can get. However, I can’t check for a class, because I guess each gameObject belongs to class MonoBehaviour or something and then it has components that belong to different classes as well.

On my door object I used the following code to make it only open for humans with the right security clearance.

    private void OnTriggerStay(Collider other)
    {
        if (other.GetComponent<Person>() != null) // Only open if someone with "Person" is near
        {
            Person collPerson = other.GetComponent<Person>(); // Get the person info from whoever is near
            if (collPerson.securityClearanceLvl >= SecurityLvl) // Only open if security level is high enough
            {
                openTimer = Time.time + openDuration;
                _animator.SetBool("SomeoneNear", true);
                colliding = other.name; // Just for testing purposes
            }
        }     
    }

If I didn’t do that, the door would keep opening by itself (but only if a rigidbody was attached for some reason, uhh…)
Now the above code seems to work as intended, but I still wonder if that’s the right way to do things. As it is, I put a “Person” component on both the player and another NPC. However, I’d rather have the NPC have a dedicated NPC script and the player have a player script, both of which might inherit from said Person class. But then I wouldn’t have the “Person” component on either of them and thus nothing to check for in the function above.

I’m not sure if I managed to explain my predicament, because I’m not asking for the solution to a specific problem. My hope is that someone with more experience can maybe shed some light on the right approach to these things in Unity and that will hopefully avoid a lot of tears and frustration in the future.

I believe something like this is what you’re looking for.

On a side note, you can remove the redundant GetComponent call in your code by moving it before the if-statement:

private void OnTriggerStay(Collider other) {
   Person collPerson = other.GetComponent<Person>();

   if(collPerson != null) {
      //etc...
   }
}

Thanks, that looks interesting. I wonder though, before I can use that function to check wether a certain class is a subclass of, say “Person”, I’d have to do a GetComponent of that specific component and assign it to a variable, right?
Let’s say I have an NPC who doesn’t have the Person script/component on it, but instead a script named NPC that inherits from that class. Then in the doors OnTriggerStay I still need to look for something to determine whether it’s a person or not. I can hardly do GetComponent and check if it’s a subclass of Person because then if the player would move near the door, GetComponent woudn’t get me anything. :frowning: