- Home /
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?
Indeed, this is an absolute basic of using Unity. You do it in basically every script. Burnumd exactly explains how to do it in the answer below. It's the first thing you do in almost any script in any Unity project.
Answer by burnumd · Feb 04, 2011 at 10:15 PM
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 ..
daBoss.UserSelectedRockets(3,"turbo");
// 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.
You can access the game object through the returned component.
What Jesse said. Editing my answer to better reflect that...
Thanks Burnurd. BTW, how to access gameobjects of FindObjectsOfType ?
Answer by ArtOfWarfare · Dec 12, 2015 at 06:30 PM
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>();
the perfect answer to a question with ~1000 of exact duplicates. sent reward points :)
Tho this does not answer the OP. Four years ago, he wanted all objects not just the first one. So it should be FindObjectsOfType, plural.
Could be confusing for someone co$$anonymous$$g in, looking at this for being shorter answer than the other one and does not get it to work as expected. Then comes stress, anger, insult, doubting about personal skills, social challenges, loneliness, schizofrenia, mass murder in a school and suicide.
So add that s and save the kids.
Answer by Graphics_Dev · Nov 04, 2016 at 12:04 PM
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.)