- Home /
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();
}
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
Straight forward enough, I just changed the WHILE to check if .Length is 0, and keep looping until it isn't.
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.
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...
@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
Follow this Question
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