Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
20
Question by kennypu · Feb 04, 2011 at 10:11 PM · gameobjectcomponent

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Nov 18, 2015 at 06:31 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
36
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jesse Anders · Feb 04, 2011 at 11:05 PM 1
Share

You can access the game object through the returned component.

avatar image burnumd · Feb 07, 2011 at 02:27 PM 0
Share

What Jesse said. Editing my answer to better reflect that...

avatar image Kumar_saki · Apr 12, 2017 at 08:16 AM 0
Share

Thanks Burnurd. BTW, how to access gameobjects of FindObjectsOfType ?

avatar image
11

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>();
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Dec 12, 2015 at 07:25 PM 0
Share

the perfect answer to a question with ~1000 of exact duplicates. sent reward points :)

avatar image fafase · Dec 12, 2015 at 07:29 PM 4
Share

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.

avatar image
9

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image varian_wrynn_logosh · Apr 24, 2020 at 02:11 AM 1
Share

Thx this post helped me after 4 years lol

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to get all scripts attached to a gameojbect? 1 Answer

Get Component on Instantiated Object 1 Answer

Script Component unchecks itself prior to build. 1 Answer

Disable/Enable Script or Add/Remove? 1 Answer

How to attach a prefab to a script that is added dynamically 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges