Unity exporting only one value to json?

I’m making a Unity Game with TikTokLive Unity library, it’s my first time using Dictionaries and Jsons in Unity so I might be doing something very wrong…. The whole code I will be sending below is included in the GiftRow class.

This is the initialization of the dictionary

public class GiftRow : MonoBehaviour
{
        private Dictionary<string, int> usersCoinsTotal = new Dictionary<string, int>();
        private void Awake()
        {
            LoadFromFile();

        }

Here is the Init function that is called somewhere else when someone gifts

public void Init(TikTokGift gift)
{
    OnGiftUpdateCoins(gift.Sender.UniqueId, GetGiftValue(gift.Gift.Name));
    Gift = gift;
    Gift.OnAmountChanged += AmountChanged;
    Gift.OnStreakFinished += StreakFinished;
    txtUserName.text = $"{Gift.Sender.UniqueId} sent a {Gift.Gift.Name}!";
    txtAmount.text = $"{Gift.Amount}x";
    RequestImage(imgUserProfile, Gift.Sender.AvatarThumbnail);
    RequestImage(imgGiftIcon, Gift.Gift.Image);
    // Run Streak-End for non-streakable gifts
    if (gift.StreakFinished)
        StreakFinished(gift, gift.Amount);
}

OnGiftUpdateCoins and GetGiftValue functions

private void OnGiftUpdateCoins(string username, int coins)
{
    if (usersCoinsTotal.ContainsKey(username))
    {
        usersCoinsTotal[username] += coins;
        Debug.Log($"{username}'s coin value updated to: {usersCoinsTotal[username]}n");
    }
    else
    {
        usersCoinsTotal.Add(username, coins);
        Debug.Log($"{username}'s coin value created as: {coins}n");
    }
    ExportSaveObjectsToJson();
}

public int GetGiftValue(string giftName)
{
    if (giftValues.ContainsKey(giftName))
    {
        return giftValues[giftName]; // Return the coin value if the gift exists
    }
    else
    {
        Debug.LogWarning("Gift not found: " + giftName); // Log a warning if the gift is not in the dictionary
        return 0; // Return 0 if the gift doesn't exist
    }
}

ExportSaveObjectsToJson function

public void ExportSaveObjectsToJson()
{
    // Convert the list of SaveObject to JSON string
    string jsonString = JsonConvert.SerializeObject(usersCoinsTotal, Formatting.Indented);

    // Define the path to save the JSON file
    //string path = Application.persistentDataPath + "/saveObjects.json";
    string path = "D:/saveObjects.json";

    // Write the JSON to the file
    File.WriteAllText(path, jsonString);

    Debug.Log("Save objects exported to JSON file at: " + path);
}

Also json doesnt save every time I close the game, for which I created this function

private void OnApplicationQuit()
{
    Debug.Log("Game quit");
    //foreach (var i in usersCoinsTotal.Values)
    //{
    //    Debug.Log(i);
    //}
    ExportSaveObjectsToJson();
    foreach (var kvp in usersCoinsTotal)
    {
        Debug.Log($"{kvp.Key}: {kvp.Value}");
    }
}

Thanks for reading this far 🙂

Issue is when I run the game, it connects to the stream successfully and the game shows all the gifts, however when I close the game, there is only one value in the dictionary (last gifter, username and coins he sent, not total coins added up, but just last gift amount of coins), same is in the json, just one gifter is shown.

Here is the whole json file, this is the last gifter before I closed the game:

{
  "девочка1234590": 1
}

I tried leaving the initialization of dictionary in class and create like ‘new’ dictionary in start and Awake functions, in that case no gifts get saved, it just doesn’t work at all in that case. I want the code to basically save the username and if the username isn’t already inside of the dictionary create a new key and value for it. If it however is there, then just update the existing value for that username.

New contributor

Granny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật