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).