- Home /
How to add a disabled Component
I want to add a component in its disabled state. When you add a component using addComponent the component is added and runs. I want to add a component and not have it enabled until later.
Answer by robertbu · Mar 08, 2013 at 10:53 PM
You can set the the enabled flag of the component to false right after you add the component:
TestStart ts = (TestStart)gameObject.AddComponent<TestStart>();
ts.enabled = false;
Awake() will be called, but the Start() call will be put off until the component is enabled. So you can put logic that you want to wait to execute in Start().
Unfortunately this results in OnEnable being called twice, once when the script is added and once when it is reenabled. This isn't a problem if you're not using the OnEnable method, which may be most cases, however, there is another (albeit terrible) solution.
You can add this.enabled at the end of the Awake of the script which is being added during runtime. Of course, there is a serious issue with this method, as it means all scripts of that type will be disabled automatically when created, even if they're not suppose to be. So basically you could only use this method on scripts which you ALWAYS want to start disabled, and which you had some method of enabling.
Also robertbu, I don't think you need the (TestStart) cast in the above code. I believe the generic version of AddComponent returns the type T.
Actually this doesn't work right for colliders. They will always be enabled in the first frame and will react with other colliders around them. You set active = false on the gameobject, but this is a big problem if you want to add a collider as a component onto another object that must stay active.
Answer by Narmer · Jun 09, 2015 at 12:49 PM
In TestStart's Awake function put:
this.enabled = false;
Answer by punk · Aug 29, 2019 at 04:39 AM
This is old, but the way I dealt with it was to add a new GameObject, then Disable it, then add my components and finally Enable it, this way all of the scripts get executed in the order you would expect and you can set whatever variables you need before Awake.
GameObject _RuntimeComponents = new GameObject();
_RuntimeComponents.name = "_RuntimeComponents";
_RuntimeComponents.transform.SetParent(gameObject.transform);
_RuntimeComponents.SetActive(false);
fsmAnimator = _RuntimeComponents.AddComponent<FsmAnimator>();
fsmAnimator.animator = animator;
_RuntimeComponents.SetActive(true);
Answer by pointcache · Oct 04, 2016 at 10:55 PM
Thanks to @Narmer for only working solution expanding on it this is what i did : The component below is designed to work with manual initialization and normal mode in editor. Manual means the component will be added and will be disabled, awaiting the "config" to be provided, then enabled. This is achieved by @Narmer s tip in awake. However because you disable the object in awake it calls OnDisable, so you have to make sure you skip that call by using "initialized" boolean that is set true in OnEnabled. This allows for component to have manual and auto initialization.
/// <summary>
/// Component has to be enabled from outside, because it starts disabled for delayed initialization
/// </summary>
public class InputBlockerComponent : MonoBehaviour {
public InputBlockerConfig config;
/// <summary>
/// Component will work normally if this is active, otherwise it will be disabled in awake
/// </summary>
public bool StartByMyself = false;
bool initialized;
void Awake()
{
if(!StartByMyself)
enabled = false;
}
void OnEnable()
{
InputBlockersSystem.RegisterBlocker(this);
initialized = true;
}
void OnDisable()
{
if (!initialized)
return;
InputBlockersSystem.UnRegisterBlocker(this);
}
}