How to prevent a developer from adding a script several times to the same gameobject?

For a particular script, I would like to prevent it from being attached more than once per gameobject.

When you try to add (for example) a Rigidbody component to a GameObject that already has one, a dialog box appears with the following text:

Can’t add the same component multiple times!
The component Rigidbody can’t be added because GameObject already contains the same component.

image

Is it possible to have this or similar behaviour for custom components, i.e. scripts?

Thank you!

add [DisallowMultipleComponent] to the top of your MonoBehaviour class declaration
like this :

[DisallowMultipleComponent]
public class MyCustomBehaviour : MonoBehaviour { }