Enable/disable dynamic Component in C Sharp script

Im trying to create component controller where i can add component/enable/disable them via script. To give idea why its like that i will tell use of it.
I have lot of Camera filter scripts and to make it possible to execute these filters ingame its much more efficient to just drag in some filters in control script for testing then changing them and so on… But atm i cant do it because i have to know what type of script/component it is and its very ineffective way if i have to change code everytime :frowning:

What i tried to do and failed in this code.
None of these lines give me access to Behavior properties like “enable”.
Is there some way how to achieve what i want or i have to go some entirely other way ?

public Component[ ] comps; // assign in editor

public void EnableComponent(int i){
comps .enabled = true; // enable not supported
GetComponent (comps .GetType ()).enabled = true; // enable not supported
}

comps .enabled won’t work because it is an array containing objects of type Component. Not only that, you have to have array of type MonoBehaviour, because Component does not have enabled property.

You can however try something like:

public MonoBehaviour[] comps;
public void EnableComponent (int index)
{
     if (index >= comps.Length || index < 0)
     {
          Debug.LogError ("Index "+index+" invalid!");
          return;
     }

     EnableComponent (comps[index]);
}

public void EnableComponent (MonoBehaviour comp)
{
     foreach (MonoBehaviour c in comps)
     {
          c.enabled = c == comp;
     }
}
1 Like