Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unity3D: Snippet that prevents adding a component multiple times to a GameObject in the Unity Editor.
#if 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(), gameObject.name),
"Cancel");
DestroyImmediate(this);
}
}
protected virtual void Reset() {
Invoke("SingleComponentCheck",0);
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment