Unity 3D c# and lack of Tuple

Unfortunately the compiler used for Unity 3D does not support Tuples.  Tuples are a convenient way to lump a couple of types together into one object without creating a class.

So, if like me you needed a list to contain a string and a float for one entry, you could do so like this:

    public List<Tuple<string,float>> things;

Then you could access the parts of the item, add both parts at the same time, etc.

There are a bunch of class definitions available that will let you use something very similar to Tuple in Unity.  I didn’t want to go to that effort, so I ended up with using an ArrayList.  This will hold multiple objects of different types in a nice ordered array.

    public List<ArrayList> things;

Now each item in the list is an array and I can get at the contents using normal array syntax.

     ArrayList thing = things[0];
     label.text = thing[0as string;
     duration = (float)thing[1];

There’s no error checking here, but you get the basic idea.

Leave a Reply