- Home /
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
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"
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.
i Tried your way, of using reset, but for me don't work well. Can you give me an simple example?
[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)");
}
}
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
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
Follow this Question
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