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 boddole · Dec 18, 2014 at 08:42 AM · c#nullfindfindobjectsoftype

Trouble with GameObject.FindObjectsOfType

Hello everyone, I'm having an issue where GameObject.FindObjectsOfType returns as !null even when no objects of that type exist in the scene.

In Detail:

As used in the script below, even when no objects of the type being searched for exist, somehow the result of the search is never null. I've used this type of approach before and have not had this issue.

Any ideas on why this is happening are appreciated.

     //needed because UI elements exists before the data they need to work with:
     private IEnumerator GetReferences()
     {
         Debug.Log("GetReferences() coroutine started");
 
         while(GameObject.FindObjectsOfType<Ship_Module_Master>() == null)
         {
             Debug.Log("UI_GridSizer GetReference() still needs references");
             yield return null;
         }
 
         Debug.Log("GameObject.FindObjectsOfType<Ship_Module_Master>() has returned !null");
         
         //now, find the player master module:
         ship_Module_MasterArray = GameObject.FindObjectsOfType<Ship_Module_Master>();
         for(int counter = 0; counter < ship_Module_MasterArray.Length; counter++)
         {
             if(ship_Module_MasterArray[counter].shipControlledBy == ShipControlledBy.Player)
             {
                 Debug.Log ("playerShip_Module_Master reference found");
                 playerShip_Module_Master = ship_Module_MasterArray[counter];
                 moduleCounter += 2; //hull and engine...
             }
         }
         
         //now, use Player master module to find number of gun modules on the player's ship:
         ship_Module_GunArray = playerShip_Module_Master.GetComponentsInChildren<Ship_Module_Gun>();
         UI_Repair.ui_Repair.PlayerShip_Module_GunArray = ship_Module_GunArray;
         for(int counter = 0; counter < ship_Module_GunArray.Length; counter++)
         {
             Debug.Log("adding 1 gun module to UI");
             moduleCounter += 1;
         }
         
         InstantiateModuleBoxes();
         SizeAndSortGrid();
     }
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
1
Best Answer

Answer by noonche · Dec 18, 2014 at 10:33 AM

It appears to always return an array object. That array object can be of length zero.

Maybe something like:

 GameObject.FindObjectsOfType<Ship_Module_Master>().Length != 0
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 boddole · Dec 19, 2014 at 06:22 AM 0
Share

@noonche:

Straight forward enough, I just changed the WHILE to check if .Length is 0, and keep looping until it isn't.

avatar image fafase · Dec 19, 2014 at 09:36 AM 0
Share

It probably does something like this:

 public FindObjectsOfType<T>()
 {
      List<T> list = new List<T>();
      foreach(GameObject o in currentScene.AllActiveObjects())
      {
           if(o.GetComponent<T>() != null)list.Add(o);
      }
      return list.ToArray();  // Even if list is 0 you get something
 }

So in the end you always get the reference to point somewhere, whether it is an empty array.

So the Length is the solution.

avatar image fafase · Dec 19, 2014 at 09:39 AM 1
Share

Now a better approach ins$$anonymous$$d of checking and waiting for something, you should look into event and subscriber. The subscriber, this class in this case, registers itself to an event defining that the data are ready. Then the event is called when all done. $$anonymous$$aybe, based on what you are after you cannot do that though...

avatar image Bunny83 · Dec 19, 2014 at 10:16 AM 0
Share

@fafase: FindObjectsOfType is actually a bit more complex since it can find any type derived from UnityEngine.Object (including $$anonymous$$aterials, $$anonymous$$eshes, Components, GameObjects, Textures, ...).

However, you're probably right that it simply uses a List (or a C++ vector since that method belongs to native code) to gather all objects and finally return that as array. If the list is empty you will get an empty array back.

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

29 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 avatar image avatar image avatar image avatar image

Related Questions

What is the best way for scripts to find the player C# 2 Answers

Distribute terrain in zones 3 Answers

C# The call is ambiguous between the following methods or properties 1 Answer

Multiple Cars not working 1 Answer

A node in a childnode? 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