Question about RequireComponent()

Is it possible to make this conditional? I want to design game items such that an item parent script has an array of item data subtypes (for example, weapon data, or consumable data). I want to set it up so, if I add an item subtype to the array, it will automatically add the appropriate script to the object rather than having to remember to manually add the script.

So, let’s say I am creating a chocolate sword object. I add the item parent script to it. In the inspector, I set the item subtype array to 2 elements and add ‘weapon’ and ‘consumable’ as those subtypes. Ideally, I would like, then, for the weaponData and consumableData scripts to be automatically added to the object by dong this. This seems like RequireComponent functionality, but I’m not sure you can even do conditional checking using that. Is there anyway to achieve this sort of functionality, or am I just going to have to remember to add the scripts myself?

To make it “conditional” use Start to check if the component is there and if not, add it.

If use use an attribute like RequireComponent its enforced and required to be always valid, not just “if XXX”

That’s what I figured. Can’t use Start() because it is to make adding content faster when building the world, not anything that happens at runtime.

In that case, one option would be to use an editor script (for example, a menu command that adds the appropriate components to the currently selected game objects).

Either that or use ExecuteInEditMode attribute

I was wondering if ExecuteInEditMode was an option. I’ll give that a try and see what happens.