3
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Does the 100 object group have common parent object (unique to this group)? Do you need to turn off functionality or completely detach it? \$\endgroup\$ Commented Oct 29, 2015 at 12:09
  • \$\begingroup\$ @wondra it can do, it would just be an empty game object though, or i can use one of the group as a parent. It wuold just be to turn off a script for each object within the group \$\endgroup\$ Commented Oct 29, 2015 at 12:12

2 Answers 2

1
\$\begingroup\$

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.

\$\endgroup\$
0
2
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ Thanks @wondra I want to avoid adding more scripting to my project, but ty for response :) \$\endgroup\$ Commented Oct 29, 2015 at 13:29
  • \$\begingroup\$ @CH99 in that case you can make private bool IsThisEnabled = false; static(and not query it from parent), but it will turn off script on ALL objects regardless of group. \$\endgroup\$ Commented Oct 29, 2015 at 13:39

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.