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
3
Question by Dino000 · Mar 29, 2015 at 07:49 PM · findobjectsoftype

How to get all components in scene which inherit a class

How to get all components from gameobjects in scene where those components inherit the same class?

For example: FindObjectsOfType(); // I'd like it to return components of type MachineGun, GrenadeLauncher, etc. which inherit CarWeapon

Comment
Add comment · Show 3
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 vintar · Mar 29, 2015 at 08:51 PM 0
Share

check here, it may help :

link text

avatar image Dino000 · Mar 29, 2015 at 08:56 PM 0
Share

@vintar I am looking for Unity solution

avatar image vintar · Mar 29, 2015 at 09:00 PM 0
Share

if you use C# then I just gave you the solution?

 $$anonymous$$achineGun[] mgs = FindObjectsOfType<$$anonymous$$achineGun>();
 foreach($$anonymous$$achineGun mg in mgs)
 {
     if(typeof(mg).IsSubclassOf(typeof (CarWeapon)))
     {
         //yes it inherits from CarWeapon
     }
 }

I believe this should work.

3 Replies

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

Answer by Huacanacha · Mar 29, 2015 at 09:11 PM

For components on active GameObjects:

 BaseClass[] components = GameObject.FindObjectsOfType<BaseClass>();

For all active and inactive:

 BaseClass[] components = Resources.FindObjectsOfTypeAll<BaseClass>();

These will find all BaseClass components as well as derived classes. In your case BaseClass would be CarWeapon.

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 Dino000 · Mar 30, 2015 at 07:34 PM 0
Share

Thanks. I was doing that, but didn't realized I was getting an error before it was called, so it didn't even get to that point. Dumb mistake.

avatar image
2

Answer by DanBarreno · Jul 24, 2020 at 07:16 AM

Active and inactive components:


     List<GameObject> rootObjectsInScene = new List<GameObject>();
     Scene scene = SceneManager.GetActiveScene();
     scene.GetRootGameObjects(rootObjectsInScene);
 
     for (int i = 0; i < rootObjectsInScene.Count; i++)
     {
         BaseClass[] allComponents = rootObjectsInScene[i].GetComponentsInChildren<BaseClass>(true);

         for (int j = 0; j < allComponents.Length; j++)
         {
             allComponents[j].DoSomething();
         }
     }



required namespaces:

  • using UnityEngine;

  • using UnityEngine.SceneManagement;

  • using System.Collections.Generic;

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 Schmit · Nov 16, 2020 at 10:03 PM 0
Share

Exactly what I was looking for - didn't occur to me to travel from the active scene.

Nifty LINQ equivalent:

 var baseComponents = Scene$$anonymous$$anager.GetActiveScene()
                 .GetRootGameObjects()
                 .Select$$anonymous$$any(gameObject => gameObject
                     .GetComponentsInChildren<BaseClass>())
                 .ToList();


avatar image
0

Answer by jean-noellevels3d · Sep 04, 2017 at 07:22 AM

Unfortunately, it does not work with other classes than Unity Objects... So if you want all interfaces derived classes, forget.

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 Bunny83 · Sep 04, 2017 at 07:34 AM 2
Share

It does work with interfaces, however you probably have to use the non generic version that takes a System.Type as parameter. The generic parameter has a constraint to UnityEngine.Object

ps: of course the method can only find either $$anonymous$$onobehaviour derived or ScriptableObject derived classes which implement that interface. "Normal" classes are not trackable classes.

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

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

FindObjectsOfType not find all objects and code not do anything 0 Answers

Where is ScriptableObject.CreateInstance stored? 2 Answers

FindObjectsOfType for abstract generic class 0 Answers

Find all components of specific type, as fast as possible 0 Answers

FindObjectsOfType doesn't find instantiated objects 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