Enabling and Disabling Scripts in Children

I need to enable and disable all scripts in the children of a parented game object. Some objects are the parents of other children also. I need it to be fast and seamless so I don’t think I can use gameObject find because there are a lot of objects. Is this possible?

I know about gameObject.SetActiveRecursively(false) but it glitches when I enable it. Enough to where it’s really noticeable.

If anyone knows how I can accomplish this that’d be great. I’m using Unity 3.5 not Untiy 4.

Use a static collection (a list, for example) to contain references to all these objects, then call a function/method that will iterate through that collection, and set each of these objects to disabled. The list’s type should likely be something general, if you’re working with a good variety of different types. So, at the scene’s loading, have all the objects add themselves to this list (or use a Find call to find them all, at the start of the scene). You should only need to do this once, so from there on in, getting access to -all- of them will be fairly simple.

GetComponent calls also tend to be fairly costly, so keeping this “manifest” of these objects (and making sure you’ve kept track of the locations of its members, within it, so you know which ones to activate/deactivate) will improve speeds dramatically.

For example:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ActivationManager{
    public static List<GameObject> goList;
    public static List<SomeBehaviour> sbList;
    // adding lists as needed

    void Awake()
        goList = new List<GameObject>((GameObject[])FindObjectsOfType(typeof(GameObject)));
        sbList = new List<SomeBehaviour>((SomeBehaviour[])FindObjectsOfType(typeof(SomeBehaviour)));
        // repeat for additional types            

The fastest way would be to SetActive on a parent object of the scripts to disable. For instance attach them all to an empty game object within the parent and just set that to inactive which will in turn control that empty object and all of its children.

You can find all components that can be “disabled” by doing:

   var canBeDisabled = parent.GetComponentsInChildren<Behaviour>();

You can get an array of all children (and the parent) by doing:

  var allObjects = parent.GetComponentsInChildren<Transform>();