- Home /
Only one component of type on a Gameobject at a time
Does Unity API provide a simple solution that will allow me to only allow a single script of a certain type on a Gameobject in the editor like most of the built in components?
Answer by VesuvianPrime · Feb 26, 2015 at 12:59 PM
I'm not sure how recently this was added, but there is now the class attribute:
[DisallowMultipleComponent]
This will prevent the editor from letting the user add the Component to the same GameObject twice.
Answer by Bunny83 · Jul 23, 2011 at 11:34 PM
Well, not really. The best you can do is to use the Reset() event to detect duplicates. But be careful, you can't destroy the instance in Reset().
See this question for a detailed explanation / solution:
http://answers.unity3d.com/questions/63826/replace-existing-component.html
I didn't think so. I was going to write my own helper function but probably would have run into problems if you hadn't pointed towards that question. Thanks