Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by Raigex · Jun 27, 2012 at 09:58 PM · gameobjectvuforiagetcomponents

how to find all components of a game object

I was wondering if it is possible to obtain all the components that a game object has and output their names. I'm asking because I have an ImageTarget (Vuforia SDK) and it is being created Dynamically so I want to know which components are already part of the gameobject and which I will want to dynamically add.

Comment
Add comment
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

2 Replies

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

Answer by Wolfram · Jun 27, 2012 at 10:02 PM

Why not just use GetComponents()?

 var allComponents : Component[];
 allComponents = gameObject.GetComponents (Component);
 for (var component : Component in allComponents) {
     Debug.Log(component.name);
 }

Note this will include the Transform, too. To also get the Components of all children, use GetComponentsInChildren()

EDIT: Umh, wait...components don't have names, the loop will just print the GameObject's name over and over again. What exactly do you mean? Do you want to check their types, to see whether you need to add a particular type?

EDIT2: Example how to figure out which components are not attached yet, and attach them.

 var allComponents : Component[];
 allComponents = gameObject.GetComponents (Component);

 // example: check for MeshRenderer and Material, replace/extend with the components you need
 var foundMeshRenderer:bool=false;
 var foundScriptOfType_MyScriptType:bool=false;
 for (var component : Component in allComponents) {
     if(component.GetType()==MeshRenderer)
         foundMeshRenderer=true;
     else if(component.GetType()==MyScriptType) // use an existing script's name here
         foundScriptOfType_MyScriptType=true;
 }
 if(!foundMeshRenderer)
     gameObject.AddComponent(MeshRenderer);
 if(!foundScriptOfType_MyScriptType)
     gameObject.AddComponent(MyScriptType);
 

There are more efficient and better ways (for example, using a List and/or array.Contains(), but I don't know the exact syntax for UnityScript, since we program in C# exclusively.

Comment
Add comment · Show 5 · 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 Raigex · Jun 28, 2012 at 01:28 PM 0
Share

Yes this is exactly what i want, i want to be able to see what components are already attached to the gameObject currently found and which components need to be added

avatar image Wolfram · Jun 28, 2012 at 01:56 PM 0
Share

I edited my answer accordingly.

avatar image Raigex · Jun 28, 2012 at 02:30 PM 0
Share

Well im using C# for unity so that i understand a bit more than javascript. But thanks. I followed your previous way and got the same except using if(gameObject.GetComponent() == null) { //add component code }

avatar image razcrux · Jul 04, 2014 at 06:03 PM 0
Share

Any chance this code could be provided in C# ins$$anonymous$$d of JS? I'd like to debug and see all components of a loaded object but I'm not sure how to do that. Thanks!

avatar image sokkyoku · May 15, 2015 at 01:18 PM 0
Share

@razcrux here you go :

 bool found$$anonymous$$eshRenderer = false;
 bool foundScriptOfType_$$anonymous$$yScriptType = false;
 
 foreach (var component in gameObject.GetComponents<Component>()) {
   if (component.GetType() == typeof($$anonymous$$eshRenderer)) {
     found$$anonymous$$eshRenderer = true;
   } else if (component.GetType() == typeof($$anonymous$$yScriptType)) {
     foundScriptOfType_$$anonymous$$yScriptType = true;
   }
 }
 if (!found$$anonymous$$eshRenderer) {
   gameObject.AddComponent<$$anonymous$$eshRenderer>();
 }
 if (!foundScriptOfType_$$anonymous$$yScriptType) {
   gameObject.AddComponent<$$anonymous$$yScriptType>();
 }

Sorry if this is considered necroing, I believe that this contributes to the quality of the answer.

avatar image
1

Answer by paver · Oct 10, 2013 at 10:34 PM

For some reason, when I put in component.GetType rather than component.name it gave me the names of my components:

 var allComponents : Component[];
 allComponents = gameObject.GetComponents (Component);
 for (var component : Component in allComponents) {
 Debug.Log(component.GetType());
 }
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 Raigex · Oct 11, 2013 at 06:42 PM 0
Share

that is a statement. Please do not post statements like that as an answer. use comments.

avatar image Edy · Jan 17, 2014 at 06:05 PM 0
Share

Exactly what I needed, thanks.

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

7 People are following this question.

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

Related Questions

Why my object is ''destroyed'' when it's actually still in the scene? 0 Answers

GUI Button On Click Event 0 Answers

Timer for destroying/ appearing Gameobject (Vuforia) 2 Answers

Stack Overflow When Trying to Call an Android Activity 0 Answers

Loading AssetBundles from inside the Project 1 Answer


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