Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
3
Question by OutplayHH · Jan 08, 2015 at 01:48 PM · editorbuttoncomponentreplace

How to replace the Button component in the editor with a subclassed version

I am trying to replace a component UnityEngine.UI.Button with my own sub classed component CustomButton (which derives from UnityEngine.UI.Button).

This is all working but one thing that is annoying and I want to fix is that in order to replace an existing component UnityEngine.UI.Button with my new one CustomButton in the editor I have to delete the UnityEngine.UI.Button component before adding the CustomButton. This is annoying as I lose all the properties set in the UnityEngine.UI.Button component which apply to the CustomButton, so I have to input the values in again.

If I just try to add CustomButton while a UnityEngine.UI.Button is already attached I get an error box from the editor:

Can't add script Can't add 'CustomButton' to ButtonName because a 'Button' is already added to the game object! A GameObject can only contain one 'Selectable' component

What I would like to do is intercept this error store the values of UnityEngine.UI.Button, then remove the component, add my new CustomButton component and set it's values.

First can I intercept this error message, or the action of adding a new component before anything is added? How would I go about doing this?

I've not had experience with creating editor scripts yet, but I am a competent coder who uses C#.

Thanks in advance for any help

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
1

Answer by ModLunar · Jul 21, 2019 at 12:26 AM

Sorry this is very late! At the top-right of your Inspector, there are 3 little bars. Click them, and you can change to Debug Mode Inspector. This will draw even most private variables, ignoring custom PropertyDrawers, and fun fact: It will even expose their real names if you hold "Alt" on the keyboard.


Getting into Debug Mode Inspector
Once you're in Debug Mode Inspector, scroll down to the Button Component, and you'll be able to change the "Script" there. Beware that you won't be able to find the Button component again through here. The only way to change it back with this, would be to Ctrl + Z/Undo. But you should be able to find your component just fine here.

Button Component shown in Debug Mode Inspector


You can get back to the default Inspector (out of Debug Mode) by clicking the "Normal" option in the first screenshot above.

I hope this helps! You can use this to switch scripts anywhere, pretty much!


switch-button-component-type-debug-mode.png (18.2 kB)
getting-into-debug-mode-inspector.png (13.3 kB)
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 Mollrow · Apr 17, 2020 at 08:07 PM 0
Share

This works amazingly, thank you! Should definitely be marked as the correct answer.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Replace Existing Component? 2 Answers

Add two components of the same type using RequireComponent attribute 1 Answer

Only one component of type on a Gameobject at a time 2 Answers

GUI.Button works in editor but not on Mobile device 1 Answer

RequireComponent at variable level? 1 Answer


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