S
S
SkyCrusher2021-04-11 13:46:42
Unity
SkyCrusher, 2021-04-11 13:46:42

Resources.FindObjectsOfTypeAll not working on Unity startup?

I want to make a singleton based on ScrictableObject.

public abstract class ScriptableSingleton<T> : ScriptableObject where T : ScriptableObject {
        static T _instance = null;
        public static T Instance
        {
            get
            {
                if (!_instance)
                    _instance = Resources.FindObjectsOfTypeAll<T>().FirstOrDefault();
                return _instance;
            }
        }
    }

I found such code, and everything would be fine, but only when Unity is restarted, when accessing Instance, I get a NullReferenceException. However, it is worth highlighting the ScriptableObject itself in the project window, the code works fine. I suspect that this has something to do with the loading of the object when it is allocated to Unity's memory, but I could not find how to fix this problem. Unity version: 2020.3.2f1.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question