Get a List of the declared objects in a class

Hello,

I’m new to unity and after a bit of searching and not finding a solution, I’m hoping to find help here.

I’ve made this class:

public class Teste
    {
        public int number;
        public string name;
        public Sprite photo;
    }

It doesn’t inherit MonoBehaviour.
And I’ve created a DataBase class to create various instances of Teste:

public class TesteDB : MonoBehaviour
{
private static Dictionary<int, Teste> TesteDictionary;
public static Teste test1, test2, test3, test4, test5, test6, test7, test8, test9;
}

I want add all those Teste objects to the TesteDictionary. But I have more than 100 objects. So I was trying to get a List of the objects to make a foreach loop to add to the Dicitionary.

Simplifying:

  1. get all the declared Teste Objects
  2. put them on a list or array;
  3. loop the list or array and add each object to a Dictionary.

All my research was unfruitful. Can anyone help me?

It is particularly unconventional to store large amounts of data in code.
You would have to write to source files to persist any changes to the data, those source files would then need to be recompiled etc. For that reason and many others its just not a good idea.


You should either store them in some sort of structured file format, JSON, XML, SVG or some binary format etc or an actual database, SQL, mongo etc.


Once you have decided which one of those you want the process should be more clear as the data format will inform the model you want to deserialise/ retrieve the data into.


That said if you want to initialise a dictionary with lots of values you can either add them the normal way (i.e. instantiating a dictionary and using its Add method) or you can use the Dictionary literal i.e

Dictionary<string, MyObject> MyDictionary = new Dictionary<string, MyObject>
{
    { "My Key A", new MyObject(); },
    { "My Key B", new MyObject(); },
    { "My Key C", new MyObject(); },
};