Dictionary failed adding integer key

public class TestValidThing
{

   public int totalCount = 0;

   public Dictionary<int, UnityEngine.GameObject> donk = new Dictionary<int, UnityEngine.GameObject>
   {
       [totalCount++] = new UnityEngine.GameObject { }
   };

}

It fails with:

A field initializer cannot reference the non-static field, method, or property 'TestValidThing.totalCount'

It’s not part of a static class or anything, it’s fully independent and accessible from everywhere. It underlines the totalCount++.

Apparently the number given has to be static… I don’t get how it works, but okay, it works now.

You’d have to put that code in a function if you wanted it to work.

edit: To be honest, I don’t follow what that code is supposed to be doing, but generally when you initialize a class member it has to be something simple that the compiler can resolve. Anything more complicated than that you have to put in actual code, which needs to go in a method in your class.