Ever wondered if it was possible to store and return multiple values from a variable or method in Unity? Well its possible and that’s where a tuple comes into play. While i haven’t used a tuple myself for any particular projects i can see the benefit of using them in certain scenarios. To sum it up quickly a tuple is a lightweight data structure that can hold multiple elements of different types – it basically allows you to group related data without the need to define a specific class or struct. However, classes or structs in themselves can also be used as a tuple so its important to understand the different ways you can group up data according to your needs.

Below are three different examples of how a tuple can be structured:

//Custom structure for Sample 3.
//Can also be converted to a class if more flexibility is required
public struct PlayerInfo
    public string playerName;
    public int playerScore;

public class Tuples : MonoBehaviour
    [SerializeField] string athleteName;
    [SerializeField] int athleteScore;
    private void Start()
        //Sample 1.
        (string name1, int score1) = GetPlayerInfo();
        Debug.Log($"Player: {name1}, Score {score1}");

        //Sample 2.
        GrabPlayerInfo(out string name2, out int score2);
        Debug.Log($"Player: {name2}, Score {score2}");

        //Sample 3.
        PlayerInfo playerInfo = RetrievePlayerInfo();
        Debug.Log($"Player: {playerInfo.playerName}, Score: {playerInfo.playerScore}");

    //Sample 1. method
    private (string, int) GetPlayerInfo()
        string playerName = athleteName;
        int playerScore = athleteScore;
        return (playerName, playerScore);

    //Sample 2. method
    private void GrabPlayerInfo(out string playerName, out int playerScore)
        playerName = athleteName;
        playerScore = athleteScore;

    //Sample 3. method
    private PlayerInfo RetrievePlayerInfo()
        PlayerInfo player = new()
            playerName = athleteName,
            playerScore = athleteScore
        return player;

If you run the above code in Unity you will notice in the console that they all output the exact same data. All three of these approaches to working with grouped data are pretty memory efficient but i should mention that Sample 3 could be converted from a struct to a class if more flexibility is required…but keep in mind classes get stored in the heap.

And that’s pretty much all there is to working with tuples.