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 /
avatar image
1
Question by ManaAura · Jul 20, 2017 at 02:25 PM · editorcomponentapplicationconstructordestructible

Is there a way to detect "On Component Attach" and "On Component Remove"??

Hello,

I want to is there a "on component attach or remove" that I can use? Most importantly, I want to be able to force the things I do in those two moments ONLY IN the "Editor Time".

---Editor Time (Didn't hit play button) ---

Right after Adding a component "custom class" ==> ( Debug.Log ("Hi")) Right after Removing a component "custom class" ===> ( Debug.Log ("Bye"))

---Run Time ( Pressed the play button) --

Right after Adding a component "custom class" ===> ( // DONT DO ANYTHING) Right after Removing a component "custom class" ===> ( // DONT DO ANYTHING)

Thanks in advance

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Reynarz · Jul 20, 2017 at 02:34 PM

You can do a counter of components. Remember put the attribute [ExecuteInEditMode] But, there is not a beautiful way. Look this example:

     private Component[] _components;
     private int _actualLength;

     private void Start()
     {
          GetAllComponents();
          _actualLength = _components.Length;
     }

      private void Update()
      {
             //Do this is not apropiate, for the performance :/
              GetAllComponents();

             if(_components.Length > _actualLength)
             {
                   Debug.Log("Was attached a component");
                    _actualLength = _components.Length;
             }
             else if(_components.Length < _actualLength)
             {
                     Debug.Log("Was removed a component");
                    _actualLength = _components.Length;
             }
      }

      private void GetAllComponents()
      {
          _components = GetComponents(typeof(Component));
      }

And you can use two properties "Application.isEditor" And "Application.isPlaying"

Comment
Add comment · Show 5 · 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 Glurth · Jul 20, 2017 at 03:09 PM 1
Share

This answer is pretty good (though I don't see the need to count components), but fails to mention that: one defines the Awake() and OnDestroy() functions, in conjunction with [ExecuteInEdit$$anonymous$$ode] to in-order run those functions when a component is added or removed from an object.

However, you may ins$$anonymous$$d wish to consider using Reset() rather than Awake(), if you wish to detect a return to the initial state of the component, rather than just it's addition. Reset is ALSO called when a component is added.

avatar image Reynarz Glurth · Jul 20, 2017 at 03:28 PM 0
Share

i Tried your way, of using reset, but for me don't work well. Can you give me an simple example?

avatar image Glurth Reynarz · Jul 20, 2017 at 03:45 PM 1
Share
 [ExecuteInEdit$$anonymous$$ode]
 public class Componenttester : $$anonymous$$onoBehaviour {
 
     private void Awake()
     {
         Debug.Log("component added");
     }
     private void Reset()
     {
         Debug.Log("component reset");//called when object is added, or user selects to "reset" component.
     }
     private void OnDestroy()
     {
         Debug.Log("component removed (or object deleted)");
     }
 }


alt text

reset.png (10.9 kB)
Show more comments
avatar image
-1

Answer by Sergio7888 · Jul 20, 2017 at 02:32 PM

No, if you want to do something when a component is added you can do in the component Awake method but only for components where you can change the code.
Also to do things only in the editor you can use:

#if UNITY_EDITOR
//your code here
#endif
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 Glurth · Jul 20, 2017 at 03:15 PM 0
Share

Just to clarify: The pragma "#if UNITY_EDITOR" is used to allow the addition of code, that should NOT be included in an output game build. It is NOT necessary to use this pragma for code that you want to run in the editor. It is only needed to EXCLUDE such code from your final game-build. Right on the money re: Awake.

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

88 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

Related Questions

Event for loading component in the editor 2 Answers

Better way to call function from another script from editor script? 1 Answer

Can't capture Multiple Screenshots in one Second 1 Answer

Non-MonoBehaviour class exists in scene, constantly calling itself 2 Answers

Class inheriting Runtime Classes 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