# Custom Class groups?

**URL:** https://discussions.unity.com/t/custom-class-groups/660057
**Category:** Scripting
**Tags:** Scripting
**Created:** [March 31, 2017, 12:42pm UTC](https://discussions.unity.com/t/custom-class-groups/660057 "2017-03-31T12:42:33Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Comafly](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@Comafly](https://discussions.unity.com/u/Comafly)
#### Post date: [March 31, 2017, 12:42pm UTC](https://discussions.unity.com/t/custom-class-groups/660057/1 "2017-03-31T12:42:33Z")

</div>

Hi all. So in Unity you can attach different collider components to objects - box collider, capsule collider, mesh collider, etc.

But say for instance you wanted to reference the collider on a number of objects all with different collider types, you can just say GetComponent() and it will automatically return a collider on that object regardless of what type of collider it is.

Is there a way to set this up for custom classes? For example, I have a number of different door locks - tumbler, combination, electronic - that I have on different objects. Is there a way for me to say GetComponent() and have it return any of those lock types?

---

<div class="post-metadata">

### Author: ![Comafly](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@Comafly](https://discussions.unity.com/u/Comafly)
#### Post date: [March 31, 2017, 12:50pm UTC](https://discussions.unity.com/t/custom-class-groups/660057/2 "2017-03-31T12:50:52Z")

</div>

Well I just had a thought that maybe it had to do with inheritance. All my door locks derive from the base class Lock(). Sure enough I just did GetComponent() and it worked like a charm for every lock type.

That was easy! I’ll leave this here in case anyone searches it in the future.

---

<div class="post-metadata">

### Author: ![Kwinten](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kwinten/32/177444_2.png) [@Kwinten](https://discussions.unity.com/u/Kwinten)
#### Post date: [March 31, 2017, 12:51pm UTC](https://discussions.unity.com/t/custom-class-groups/660057/3 "2017-03-31T12:51:18Z")

</div>

What you are described is known as [inheritance](https://msdn.microsoft.com/en-us/library/ms173149.aspx?f=255&MSPPError=-2147217396)in object-oriented programming. When you create a Component in Unity, it inherits from MonoBehaviour by default.

You would need to create a base class “Lock”. Other lock types, such as “ElectronicLock” can inherit from this type. If you add the ElectronicLock Component to a GameObject, this is the instance which will be returned when you call GetComponent on that GameObject.

Edit: well, looks like you figured it out on your own.

---

<div class="post-metadata">

### Author: ![LaneFox](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/lanefox/32/42_2.png) [@LaneFox](https://discussions.unity.com/u/LaneFox)
#### Post date: [March 31, 2017, 12:59pm UTC](https://discussions.unity.com/t/custom-class-groups/660057/4 "2017-03-31T12:59:53Z")

</div>

Interfaces also provide similar functionality without forcing inheritance. You may want to check them out as well.

---

<div class="post-metadata">

### Author: ![johne5](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/johne5/32/476635_2.png) [@johne5](https://discussions.unity.com/u/johne5)
#### Post date: [March 31, 2017, 1:04pm UTC](https://discussions.unity.com/t/custom-class-groups/660057/5 "2017-03-31T13:04:41Z")

</div>

yes, you just need to setup a public enum

```csharp
public class Lock : MonoBehaviour
{
public LockTypes _locktype;

public enum LockTypes
{
     tumbler,
     combination,
     electronic
}

public LockTypes getCurrentType
    {
        get { return _locktype; }
        private set { _locktype = value; }
    }
}

```

So you should be able to do  
GetComponent().getCurrentType
