Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Feb 21, 2019 at 06:44 PM by InternalDissension for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by InternalDissension · Feb 21, 2019 at 05:59 PM · exceptionaddcomponentcatch

Is it possible to catch a can't add script error in the editor?

So here's the setup.

I have an abstract class named Alpha that inherits from Monobehaviour. I have classes Beta, Charlie, Delta, etc, that inherit from class Alpha, but are not abstract. Upon trying to attach script Alpha to a game object, unity throws the exception "Can't add script" since it's abstract. I know this is intentional.

My question is: when a user tries to add the script 'Alpha' to a game object, is it possible to catch the "Can't add script" error and notify the user to select one of the scripts that inherit Alpha?

EDIT: Don't know if it helps, but I am using Unity 2018.1.2f1

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

  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Feb 21, 2019 at 06:22 PM

No, your can't. And it would be absurd to catch it. You usually have countless of scripts / source files which may contain classes that do not even represent a component. All such scripts can not be added to a gameobject and would trigger that same error from the editor. You should better think about a proper script organisation in your folder structure. Your base class Alpha doesn't even need to be in a script with the same name. It could be in any script. The name matching requirement only applies to classes that are actually attached to gameobjects.


You can hide classes from the Add Component menu by marking them with

  [AddComponentMenu("")]

This essentially removes the script from the menu. I have never tried making the base class "internal" instead of "public". It may work as well. Though depending on the usage of the abstract base class it might not be an option to make it internal.


Of course the user can still drag the script manually to a gameobject, but as i said, that's true for any script that doesn't contain a monobehaviour. So just make sure you organise your files properly. If you have a more complex system it's common to use some sort of "core" subfolder which contains files that need to be there but the user never directly interacts with. Aside that folder you can create a folder "components" (or whatever fits your system) which only contains scripts which can be added as component.

Comment
Add comment · Show 1 · 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 InternalDissension · Feb 21, 2019 at 06:28 PM 0
Share

Yeah, I figured as much. I just wanted to see if there was a way eli$$anonymous$$ate all confusion on what script to add to the object. I suppose it'll still be pretty hard to get confused if the folders are organized in a pleasant fashion.

Thanks.

Follow this Question

Answers Answers and Comments

100 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 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 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

AddComponent not working 3 Answers

Adding a component in the editor with a script 0 Answers

GO.AddComponent just adds an empty Script 1 Answer

5.0.0b9 Addcomponent cause a crash 1 Answer

Compilation error, Unity 5 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