way to get all object with a certain component/script attached

Is there a easy and quick way to get all gameObject with a certain component or script attached?

Yup. Object.FindObjectsOfType. It returns an Object array, but you can cast that to the type you're looking for, and from that get the GameObject it's attached to with Component.gameObject.

Note too that FindObjectOfType (singular, no “s”) is incredibly useful.

For example, in a typical scene you’ll have a game object (called say “operations”) and the main “boss” script of the scene is attached to it.

From any other script in the scene, if you want to “get to” that boss script,

private MainBossScript daBoss;

// in Awake...
daBoss = (MainBossScript)FindObjectOfType(typeof(MainBossScript));
// OR IN FACT, you can just type this, using the generic:
daBoss = Object.FindObjectOfType<MainBossScript>();

// and then ..

// if for some reason you want that GameObject
GameObject holder = daBoss.gameObject;

this is tremendously easier that tediously connecting to the “operations”.MainBossScript using dragging for every little routine. It saves a huge amount of time during development, when you often “move things around”.

It goes without saying this operation is too slow to run repetitively during playtime. You must simply do it once in Awake, and then you have it forever, as shown in the code fragment above. This is a basic of using Unity. You do this in pretty much every script.

If you are looking for only one object, as is usually the case:

Use the generic signature instead and it comes out a lot cleaner - no casting needed:

daBoss = Object.FindObjectOfType<MainBossScript>();

I will give the exact code that can be used for what burnumd had eluded to.

Light[] lights = (Light[]) GameObject.FindObjectsOfType (typeof(Light));

This gets all light components in the scene. You can then, for example, turn out all of the lights:

foreach (Light light in lights)
    light.enabled = !light.enabled;

Of course, this is expandable to any type of component…just replace “Light” with the component you are trying to activate.

(BTW, I know this is an old thread, but I know people are still going to it because it is one of the first that pops up with a google search on the topic. This is more of the “saving of the kids” as fafase said.)

If you want to see them in your editor,
You can right click the script and click find all references in the scene.