- Home /
How do you prevent a base class from being addable as a script without making it abstract?
So I was working with 2D and colliders and I added a [RequireComponent(typeof(Collider2D))] to one of my scripts. Upon trying to add this script with none of the 2D colliders attached to the gameobject I get a message informing me about how I should add one of the four 2D collider components first.
I have a similar situation with a [RequireComponent(typeof(MonsterBehaviour))] class I made, where I would like the base class to not be addable and require one of the derived types. I can solve it by making it abstract. This will block it, but the message I get for Collider2D in comparison is very elegant.
I just can't seem to figure out how Collider2D does what it does. I tried using an CustomEditor for my script and Destroying the object, but that fails since at that point all the components have been added and it's not allowed to remove it at that point.
I would like to obtain the same level of elegance as Collider2D where the base class is prevented from being added with an informative message. Any pointers?