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
0
Question by sosojust · Dec 25, 2014 at 06:48 PM · objectreflectionnull

How can I get gameObject's all script components?

Is there a way to get a gameObject's all script components? For example, a gameObject is "Player", it has two script attached as components: "PlayerControl.cs" and "PlayerStatus.cs". How can I traverse all gameObject's script components? (different gameobjct has various script components..) I just want to traverse all gameObject's script components and then do a null reference check to make sure the game won't throw null reference exception... thanks in advance.

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

1 Reply

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

Answer by RudyTheDev · Dec 25, 2014 at 08:34 PM

To find all (public) Object-derived fields and check/report if they are null:

 void Awake()
 {
     #if UNITY_EDITOR
     foreach (FieldInfo fieldInfo in GetType().GetFields())
         if (fieldInfo.FieldType.IsSubclassOf(typeof(Object)))
             if ((Object)fieldInfo.GetValue(this) == null)
                 Debug.LogError("Field " + fieldInfo.FieldType.Name + " " + fieldInfo.Name + " is null!");
     #endif
 }

Note: This won't work at runtime on iOS and probably some other platforms.


Get all components (technically, scripts):

 gameObject.GetComponents<MonoBehaviour>();

Iterate with something like:

 foreach (MonoBehaviour script in gameObject.GetComponents<MonoBehaviour>())
 {
     // ...
 }

But they won't ever be null though, because they are attached properly. Perhaps this is not what your are asking? Any script that fails to reference another script would still throw NullRefExc-s.

If you are just looking for missing scripts:

 if (gameObject.GetComponent<RequiredScript>() == null)
     Debug.LogError("Missing RequiredScript!");

There are fancier (more optimized) ways to do this, but if you only do it once, it probably won't matter.

Edit: update per comment

Comment
Add comment · Show 4 · 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 sosojust · Dec 26, 2014 at 02:14 AM 0
Share

alt text

Thanks for your answer! I really didn't describe this clearly. As you can see in the screenshot, the "CardSet" missed attached. so this will cause a nullRefExc.. cause there are so many game objects. I just want to traverse all the gameObjects' $$anonymous$$onnoBehaviour component to see if any one missed initialization.

thank you again.

capture.jpg (29.3 kB)
avatar image RudyTheDev · Dec 26, 2014 at 11:30 AM 0
Share

Oh I see, you want to check the actual class's fields for null. I updated the answer. Note that reflection is slow and GetValue() (I'm like 95% sure) won't work on AOT platform, like iOS.

avatar image sosojust · Dec 28, 2014 at 05:25 AM 0
Share

Great thanks for your answer! It now works! I can check each object's $$anonymous$$onoBehaviour (scrpts) component. Reflection is really a good way. Thanks!

avatar image Iraklisss · Nov 18, 2017 at 04:40 PM 0
Share

i try to get all scripts of a certain type but i get a nullReferenceException. please check my question for more details it would be great if i could get an answer. https://answers.unity.com/questions/1433213/getcomponents-is-not-working-for-scripts.html

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

Instantiating an object returns null 0 Answers

How to modify ParticleSystem from Editor script? 1 Answer

object can't be null when highlighted in the inspector? 1 Answer

NullReferenceException: Object reference not set to an instance of an object 2 Answers

Reflecting Objects off of Boundaries/Walls 2 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