3

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?

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
DarkT
  • 169
  • 1
  • 4
  • 15
  • You have abstracted the components to the `Component` object, which does not have an `enabled` member. What is your intended result here? You can disable the whole player `GameObject` if you are trying to hide the player, rather than hiding all components individually – Easton Bornemeier Aug 07 '18 at 23:04
  • I have a switch case that I will be implementing and I want to disable a script based on the switch case. – DarkT Aug 07 '18 at 23:05

1 Answers1

7

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.

Programmer
  • 121,791
  • 22
  • 236
  • 328