Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Unity3D: Snippet that prevents adding a component multiple times to a GameObject in the Unity Editor.
void SingleComponentCheck() {
var components = gameObject.GetComponents(this.GetType());
foreach (var component in components) {
if (component == this) continue;
UnityEditor.EditorUtility.DisplayDialog("Can't add the same component multiple times!",
string.Format("The component {0} can't be added because {1} already contains the same component.", this.GetType(),,
protected virtual void Reset() {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment