I want to keep track of all GameObjects of a specific kind in a scene. For performance, I add and remove them from a List (instead of having to call FindObjectsOfType, I suppose this is more performant). Right now I’m doing this in two ways:
My GameManager singleton holds a List field containing the GameObjects
The GameObjects script component has a static List which checks in and out itself
Is there a difference in performance/best practice between these two ways of doing it. For consistency, I rather go with one, but I’m too inexperient in programming to see the consequences of them in the long run.
I try to use as few singletons as possible, best none. I usually end up with zero or one singleton in the game, but I never use it to keep track of gameObjects or classes. For classes (components) I use static lists and I usually do not keep track of gameObjects (since they are represented with aforementioned components).
Performance wise, FindObjectsOFType might be bad, depending on the number of objects in your game. It is considered a good practice if you avoid using this method. There should not be any significant difference between static list and singleton in performance. You should use what suits you best and optimize only when you stumble upon some performance issues.
To answer the “best practice part”, if you google “statics vs singletons” you find out that whatever you choose you choose bad. People do not like statics, people do not like singletons… They are not inherently bad practices, but they are sometimes overused/misused. There is no general rule, each case is specific. Use whatever you think is the right choice and after some time you will learn when to use what.