The FindObjectOfType method is no longer available

hello. I am a student taking a course in Korea.
This time, Unity has been updated to version 6.0, so I’m learning with the new version.

However, the new version deprecates the FindObjectOfType method.

‘Object.FindObjectsOfType()’ is obsolete: ‘Object.FindObjectsOfType has been deprecated. Use Object.FindObjectsByType instead which lets you decide whether you need the results sorted or not. FindObjectsOfType sorts the results by InstanceID but if you do not need this using FindObjectSortMode.None is considerably faster.’

If I need to change to another method, could you please tell me which one I should use?

Generally speaking, the FindObjectsOfType<T>'s automatic sort is unneeded, and in fact never sorts by a useful metric anyways.

FindObjectsByType<T> started getting a deprecation notice in Unity 2023. I haven’t installed 6 yet. Is this a deprecation notice (it’s still there but it may be used for now) or an obsolete notice (the FindObjectsOfType code simply won’t compile)?

You can use FindObjectsByType just like you would FindObjectsOfType.

Thank you for your response.
The error message is as shown in the picture.

I forgot to include the parameter

FindObjectsByType<Fader>(FindObjectsSortMode.None)
1 Like

I tried to follow your advice, but I ran into another problem.
To solve it, I need your advice.
What do you think I should do?

Do you recommend just dropping the version to 2022?

Please paste text rather than screenshots, whenever possible…
You’ve used FindObjectsOfType instead of FindObjectsByType() so the FindObjectsSortMode.None won’t fit.

namespace RPG.SceneManagement

{

    public class Portal : MonoBehaviour

    {

        enum DestinationIdentifier

        {

            A, B, C, D, E

        }

        [SerializeField] int SceneToLoad = -1;

        [SerializeField] Transform spawnPoint;

        [SerializeField] DestinationIdentifier destination;

        [SerializeField] float fadeOutTime = 1f;

        [SerializeField] float fadeinTime = 2f;

        [SerializeField] float fadeWaitTime = 0.5f;

        private void OnTriggerEnter(Collider other)

        {

            if (other.tag == "Player")

            {

                StartCoroutine(Transition());

            }

        }

        private IEnumerator Transition()

        {

            if (SceneToLoad < 0)

            {

                Debug.LogError("");

                yield break;

            }

            DontDestroyOnLoad(gameObject);

            Fader fader = FindObjectsByType<Fader>(FindObjectsSortMode.None).FirstOrDefault();

            yield return fader.FadeOut(fadeOutTime);

            yield return SceneManager.LoadSceneAsync(SceneToLoad);

            Portal otherPortal = GetOtherPortal();

            UpdatePlayer(otherPortal);

            yield return new WaitForSeconds(fadeWaitTime);

            yield return fader.FadeIn(fadeinTime);

            Destroy(gameObject);

        }

        private void UpdatePlayer(Portal otherPortal)

        {

            GameObject player = GameObject.FindWithTag("Player");

            player.GetComponent<NavMeshAgent>().Warp(otherPortal.spawnPoint.position);

            player.transform.rotation = otherPortal.spawnPoint.rotation;

        }

        private Portal GetOtherPortal()

        {

            foreach (Portal portal in FindObjectsOfType<Portal>())

            {

                if (portal == this) continue;

                if (portal.destination != destination) continue;

                return portal;

            }

            return null;

        }

    }

}

Thank you.
Here are some additional issues and the error messages in vs code for them.

private Portal GetOtherPortal()
        {
            foreach (Portal portal in FindObjectsOfType<Portal>())
            {
                if (portal == this) continue;

                if (portal.destination != destination) continue;

                return portal;
            }
            return null;
        }

‘Object.FindObjectsOfType()’ is obsolete: 'Object.FindObjectsOfType has been deprecated. Use Object.FindObjectsByType instead which lets you decide whether you need the results sorted or not. FindObjectsOfType sorts the results by InstanceID but if you do not need this using FindObjectSortMode.None is considerably faster.'CS0618

“It looks like we need to modify the FindObjectsOfType method in the foreach loop to use the new method, FindObjectsByType, instead. However, I’m not sure how to properly implement this change. Could you guide me?”

It’s fairly straightforward.
Everywhere you would use

FindObjectsOfType<T>() //T is a generic for any class you're looking for

use

FindObjectsByType<T>(FindObjectsSortMode.None)

Now as it happens, the Fader is a form of Singleton (being held in the PersistentObjectPrefab, and like a true Singleton, much like the Fabled Highlander There can be only one.

This means that in the case of the Fader, you really only need to use FindObjectOfType, which is not deprecated, only FindObjectsOfType is deprecated to handle sorting efficiency.

FindObjectOfType already retursn the first object found

Fader fader = FindObjectOfType<Fader>(); //No need for ByType for single object

In the case of GetOtherPortal, however, we need to transition to the FindObjectsByType

foreach (Portal portal in FindObjectsByType(FindObjectsSortMode.None)

At this time, the course has been thoroughly tested through Unity 2023.1 (by me). I haven’t had a chance to install or test the new Unity 6.0, which as I understand is still in alpha.

Generally, we don’t recommend using Alpha versions. We find it’s best to use the latest version3 (LTS) of any given Year version (don’t let 2022 fool you, 2022.3 wasn’t available till mid 2023, and 2023.3 is only just becoming available recently).

I’m not saying to definitely downgrade, just advising that it may cause additional issues to stay in 6. I just don’t know yet.

Privacy & Terms