Unity/Firestore Functions in JS: Invalid Type for conversion to variant – when passing a list of classes via a dictionary

I’m trying to create a cloud function written in JS that creates a new game.

I’m sending in a list of PlayerInfo from Unity, but every time I get an error about my data received as being an Invalid Type.

I’ve tried multiple different approaches from the C# Unity side but every time I get returned an error of either

1st approach

"Error creating game invite: Invalid type System.Collections.Generic.List System.Collections.Generic.Dictionary System.String,System.String for conversion to Variant"

2nd approach

"Invalid type PlayerInfo for conversion to Variant"
  • code for 1st C# approach
public async void CallCreateGameInvite()
{
     List Dictionary string, string  players = new List
    {
        new Dictionary { { "username", "Player1" }, { "userID", "userID1" } },
        new Dictionary { { "username", "Player2" }, { "userID", "userID2" } }
        // Add additional players here
    };

    await CreateGameInvite(players);
}
 private async Task CreateGameInvite(List<Dictionary<string, string>> players)
    {
        try
        {
            FirebaseFunctions functions = FirebaseFunctions.DefaultInstance;
            var func = functions.GetHttpsCallable("createGameInvite");
            var result = await func.CallAsync(new { players });

            Debug.Log("Game invite created with ID: "); //result.Data["gameID"]);
            // Use the gameID and perform further actions in your Unity project
        }
        catch (Exception e)
        {
            Debug.LogError("Error creating game invite: " + e.Message);
        }
    }
  • code for 2nd C# approach
List players = new List
{
    new PlayerInfo("Player1", "userID1"),
    new PlayerInfo("Player2", "userID2"),
};
[System.Serializable]
public class PlayerInfo
{

    public string username;
    public string userID;

    public PlayerInfo(string username, string userID)
    {
        this.username = username;
        this.userID = userID;
    }
}
private async void CreateGameInvite(List players)
{
    try
    {
        var requestData = new Dictionary
        {
            { "players", players }
        };

        FirebaseFunctions functions = FirebaseFunctions.DefaultInstance;
        var func = functions.GetHttpsCallable("createGameInvite");
        var result = await func.CallAsync(requestData);

        Debug.Log("Game invite created with ID: " + result.Data);
        // Use the gameID and perform further actions in your Unity project
    }
    catch (Exception e)
    {
        Debug.LogError("Error creating game invite: " + e.Message);
    }
} 
  • Here is my code for the JS cloud function:
// Class for player information
class PlayerInfo 
{
    constructor(username, userID) {
      this.username = username;
      this.userID = userID;
    }
}

exports.createGameInvite = functions.https.onCall(async (data, context) => {
    try 
    {
        const gameID = generateUniqueGameID();
  
        // List of players
        const players = data.players; // Assuming that players are passed in the request data
  
        // Create a document in the Firestore collection named GameInvites
        const gameInviteRef = admin.firestore().collection('GameInvites').doc(gameID);
        await gameInviteRef.set({ players: players });
  
        // Add the game invite to UserGameInvites collection for each user ID
        players.forEach(player => { admin.firestore().collection('UserGameInvites').doc(player.userID).collection('invites').doc(gameID).set({ gameID: gameID });
     });
  
        return { gameID: gameID }; // Return the gameID as the result
      } catch (error) {
          throw new functions.https.HttpsError('internal', 'Error creating game invite', error);
      }
  });

I’m guessing I’m missing something about how parse the ‘data’ object in the JS I’m sending from Unity.

Would very much appreciate any help!(: