Home > other >  Order of FindObjectsOfType method in Unity
Order of FindObjectsOfType method in Unity

Time:10-13

I'm using the Method FindObjectsOfType inside Unity and I want to know what the order Unity find objects? I tried to change the order of my game objects inside the hierarchy and even change the name of my game objects to 1_name, 2_otherName and still the list seems random.

Do it really random or there is an order for the search? There is no documentation about the order in the Unity website.

If someone really want, this is my relevent script:

[SerializeField] List<AreaMeshHandler> areaMeshHandlers;

void Awake()
{
     areaMeshHandlers = FindObjectsOfType<AreaMeshHandler>(true).ToList();
}

CodePudding user response:

Short answer, by InstanceID. can be seen on each GameObject in debug mode.

Tested by running this code in a scene with multiple GameObjects (some in hierarchy)

        foreach (Target t in FindObjectsOfType<Target>())
            Debug.Log($"{t.name}: {t.GetInstanceID()}");
  • Related