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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With