Unity Inspector – Turn off script for a group?

Unity Inspector – Turn off script for a group?

I have a large group of objects in Unity that can be grouped and for one instance I want to turn off a script on each one, for over 100 this can be tedious, can this be done group wise within the inspector? Instead of me going into each one and manually turning a script off?
I have tried doing this within a script but it will not work for this situation.

Solutions/Answers:

Answer 1:

As far as I know, selecting multiple gameobjects with each of them having a common (of the same type) component allows you to edit the properties of that component with the changes being applied to all of the gameobjects in the group.

Although multi-editing is disabled for several types of components, this should still work with Scripts.

You can select multiple gameobjects in the editor by CTRL+click-ing them. To select a group at once you’d need to click the first gameobject in the group and then SHIFT+click the last one.

Answer 2:

You can just add script onto parent with nothing else but bool flag isEnabled:

public ChildrenEnabler {
 public bool IsEnabled;
}

and edit the target script so it checks parent for the flag:

private bool IsThisEnabled = false;
void Start() {
 var e = transform.parent.gameObject.GetComponent<ChildrenEnabler >();
 if(e != null && e.IsEnabled) //enable logic here, e.g.:
   IsThisEnabled = true; 
}

void Update() {
  if(IsThisEnabled) {
  // original logic here, same check in every other implemented method
  }
}

alternatively, you can use Actions:

private Action UpdateAction;
void Start() {
  var e = transform.parent.gameObject.GetComponent<ChildrenEnabler >();
  if(e != null && e.IsEnabled) 
    UpdateAction = new Action(()=> { doUpdate(); });
  else 
    UpdateAction = new Action(()=> { /*literally do nothing*/ });

}

void doUpdate() {
  // original logic here, similar for each other implemented method in script
}

void Update() {
   UpdateAction.Invoke();
}

To enable/disable script, just change the IsEnabled on parent to true/false. This turns off its logic – it does not, however, completely remove the script form the object – the script will remain on the object but will do nothing.
If you need to turn off multiple different scripts separately, just change the flag to List of Type and in the scripts Start() method check if the parent list .Contains(this.Type).

References