How to make RequireComponent list possible options.
If you specify [RequireComponent(typeof(Collider))] for a MonoBehaviour, once you try adding it, you will get a message:
Adding component failed. Add required component of type 'MeshCollider' or 'BoxCollider' or [snip] to the game object 'Your Object' first.
I would like to have a similar message for one of my own components. I have an interface that is implemented by multiple classes and I would like to list them there. If I try using my interface with RequireComponent, I get a message that says that script class can not be abstract. If I have a concrete base class with RequireComponent, than it is simply added, which is not what I want. I would like to prompt user that one of the derived classes should be used.
I can see that Collider derives from Component, but my scripts must derive from MonoBehaviour. Does it mean I can not simulate this behavior? Any suggestions?
Answer by LukaKotar · Dec 18, 2015 at 06:17 AM
If the class is not supposed to be used as a component (or instantiated, for example with the 'new' keyword) at all, you can make the class abstract, which will prevent the script from being added. I am assuming you want to use inheritance, otherwise you can make it static.
If you do need to create instances of the class, I don't think you can prevent it from being added, but you can throw an error into the console and then remove the component. You can use the OnValidate() function for that.
#if UNITY_EDITOR // Only runs in editor, doesn't get compiled on build
void OnValidate () {
// Show an error in the console: (you can also do LogWarning or Log)
Debug.LogError("This script should not be added as a component! Removing...");
// Destroy the component (of course, replace "YourComponent" with the actual class)
Destroy(GetComponent<YourComponent>());
}
#endif
Thanks, by using RequireComponent(typeof($$anonymous$$yInteface)) I do not get the object added and I get a pop up saying "script class can not be abstract" (which is one of your suggestions). But I would like to see if Unity can provide me with a list of scripts I should add ins$$anonymous$$d of failing with some not useful error pop up. Currently I assert if all the required components exist in run time. That puts a message into the log.
alexander-shyrokov Pretty useful error. It clearly said that abstract classes or interfaces cannot be added because
1) unity doesn't know what class that inherits that abstract class/interface to choose,
2) abstract classes/interfaces cannot be instantiated, that is why they are abstract, so unity can't attach a component to object because it cannot instantiate it.
3) you are asking for unity to check all the classes to find the ones that inherit from that abstract class/interface and give you a list of options, so basically checking all the .dll, plugins and other stuff to find that class, it's possible, but they didn't implement it as it was not required too much. I agree that it would be a useful feature, though.
Your answer
Follow this Question
Related Questions
Inheriting from a class that inherits from monobehaviour 3 Answers
How to inherit from a class that inherits from MonoBehaviour 1 Answer
advice on resource management system architecture 2 Answers
Extending a built-in component 0 Answers
Show variable of class that dont inherit from MonoBehaviour in Inspector 1 Answer