1

I tried this:

private List<GameObject> objects;
private List<int> points;
private Dictionary<GameObject, int> objectsAndPoints;

private void Start()
{
    for (int i = 0; i < objects.Count; i++)
    {
        objectsAndPoints.Add(objects[i], points[i]);
    }
}

But it popups an error like this: NullReferenceException: Object reference not set to an instance of an object

And there aren't things like List.zip or Enumerable.Range in Unity / MonoBehaviour, they are for .net

Does anybody know how to map two array into a dictionary in Unity / MonoBehaviour

Birerdem 7
  • 11
  • 1
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – BugFinder May 09 '22 at 17:47

1 Answers1

1

Enter the initial value. Otherwise there will be no reservation space for the dictionary.

private Dictionary<GameObject, int> objectsAndPoints = new Dictionary<GameObject, int>();

Also, if your list is set from within the code, make sure its initial value is not equal to null.

private List<GameObject> objects = new List<GameObject>();
KiynL
  • 4,097
  • 2
  • 16
  • 34