Object.FindObjectsOfType error

for this lecture and last section i keep getting errors like this that say Object.FindObjectsOfType is deprecated
is anyone else seeing this?

Assets\GameDev.tv Assets\Scripts\Saving\SavingSystem.cs(89,49): warning CS0618: ‘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.’

in the SavingSystem.cs class I changed

    private void CaptureState(Dictionary<string, object> state)
    {
        foreach (SaveableEntity saveable in FindObjectsOfType<SaveableEntity>()) <---
        {
            state[saveable.GetUniqueIdentifier()] = saveable.CaptureState();
        }

        state["lastSceneBuildIndex"] = SceneManager.GetActiveScene().buildIndex;
    }

To ::

    private void CaptureState(Dictionary<string, object> state)
    {
        foreach (SaveableEntity saveable in FindObjectsByType<SaveableEntity>(0)) <---
        {
            state[saveable.GetUniqueIdentifier()] = saveable.CaptureState();
        }

        state["lastSceneBuildIndex"] = SceneManager.GetActiveScene().buildIndex;
    }

would this be right? It seems to work But I’m not an expert on the saving System.

Technically, the formal syntax is

FindObjectsByType<SaveableEntity>(FindObjectsSortMode.None); //or FindObjectsSortMode.InstanceID)

but as it happens, you can pass 0, which is equivalent to saying don’t sort it.

As the warning states, FindObjectsByType is considerably faster when the sort mode is none. In fact, if you’re using the result in a Linq expression where you will be sorting it by some other criterion anyways, that initial sort in the deprecated (as of Unity 2023) FindObjectsOfType is a complete waste of computation cycles.

ok, I did not see that until now. Thank you for your help.

Privacy & Terms