Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by alexander-shyrokov · Dec 17, 2015 at 04:41 PM · componentinheritancemonobehaviourinterfaces

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image alexander-shyrokov · Dec 18, 2015 at 05:02 PM 0
Share

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.

avatar image MaxKarpinsky · Sep 17, 2017 at 05:00 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

34 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Extending a built-in component 0 Answers

Show variable of class that dont inherit from MonoBehaviour in Inspector 1 Answer

How to automate removing non-MonoBehaviour derived scripts from a prefab 0 Answers

accessing a script using a variable with getComponent, then accessing a variable inside that script 1 Answer

Public MonoBehavior string fields empty 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges