'Component' Does Not Contain a Definition for 'Enabled' and No Extension Method 'Enabled'

I am trying to disable and disable a Component stored in a List. When I try to do so, I get the following error:

'Component' does not contain a definition for 'enabled' and no extension method 'enabled' accepting a first argument of type 'Component' could be found (are you missing a using directive or an assembly reference?)

I also tried

 components[4].SetActive(false);

and get a similar error

public List<Component> components;
...
components = new List<Component>();
components.Add(player.GetComponent<_2dxFX_HSV1>());
components.Add(player.GetComponent<_2dxFX_HSV2>());
components.Add(player.GetComponent<_2dxFX_HSV3>());
components.Add(player.GetComponent<_2dxFX_HSV4>());
components.Add(player.GetComponent<_2dxFX_Negative>());
components.Add(player.GetComponent<_2dxFX_Lightning>());
components.Add(player.GetComponent<_2dxFX_MetalFX>());
components.Add(player.GetComponent<_2dxFX_Pixel8bitsC64>());
components.Add(player.GetComponent<_2dxFX_GoldFX>());
components.Add(player.GetComponent<_2dxFX_Waterfall>());
components.Add(player.GetComponent<_2dxFX_Hologram>());
components.Add(player.GetComponent<_2dxFX_PlasmaRainbow>());

components[4].enabled = false;

How do I disable a Component type in a List?

2

1 Answer

You can't enable/disable Unity's Component type.

You are looking for Behaviour. Behaviours are Components that can be enabled or disabled.

If you store a script reference as Component and want to enabled or disable it, cast it to Behaviour then you can enable or disable it.

Replace

components[4].enabled = false;

with

Behaviour bhvr = (Behaviour)components[4];
bhvr.enabled = false;

Read this for difference between the two.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest