How can i fix the score saving method in unity3d c# project?

i managed to create a simple clicker game in unity3d.

At the beggining everything worked, clicking, score saving method etc.

Then i wanted to add a shop,that will upgrade the user mouse power click (basically more score added for every click). Right now it works and not works, When debugging, aftec buying an upgrade it says that it saved the correct score , but when im closing the shop or clicking again, it saves the score that was before buying the upgrade.

Any fixes?

StrawberryClicker.cs

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class StrawberryClicker : MonoBehaviour
{
    public Button clickButton;
    public Text clickCountText;
    public Text dropTimeText;

    private int clickCount = 0;
    private int mousePower = 1;
    private float clickTime = 0;
    private float dropTimer = 0;
    private float dropInterval = 3 * 3600; // 3 hours in seconds
    private float idleDropInterval = 18 * 3600; // 18 hours in seconds
    private bool isClicking = false;
    private float lastClickTime = 0;
    private string userId;

    void Start()
{
    clickButton.onClick.AddListener(OnClick);

    userId = LocalStorageManager.GetUserId();
    // Load the score when the game starts
    LoadScore();

    mousePower = PlayerPrefs.GetInt("MousePower", 1);

    // Automatically save score every 5 seconds
    InvokeRepeating("SaveScore", 5f, 5f);
}

    public void StartSavingScore()
    {
        InvokeRepeating("SaveScore", 5f, 5f);
    }

    public void StopSavingScore()
    {
        CancelInvoke("SaveScore");
    }

    void Update()
    {
        clickTime += Time.deltaTime;
        if (isClicking)
        {
            lastClickTime = clickTime;
            if (clickTime - dropTimer >= dropInterval)
            {

            }
        }
        else if (clickTime - lastClickTime >= 60)
        {
            if (clickTime - dropTimer >= idleDropInterval)
            {

            }
        }

        dropTimeText.text = $"Next Drop In: {(dropInterval - (clickTime - dropTimer)) / 3600:F2} hours";
    }

        // Add this variable
    public int ClickCount { get { return clickCount; } }

    // Modify OnClick method
    void OnClick()
    {
        clickCount += mousePower;
        isClicking = true;
        clickCountText.text = $"Clicks: {clickCount}";

        // Add this line to update click count in ShopManager
        FindObjectOfType<ShopManager>().UpdateClickCount(clickCount);
    }

    public void SaveScore()
    {
        LocalStorageManager.SaveScore(clickCount);
    }

    void LoadScore()
    {
        clickCount = LocalStorageManager.LoadScore();
        clickCountText.text = $"Clicks: {clickCount}";
    }
}

ShopManager.cs

using UnityEngine;
using UnityEngine.UI;

public class ShopManager : MonoBehaviour
{
    public GameObject shopPanel;
    public Button shopButton;
    public Button closeButton;
    public Button buyMousePowerButton;
    public Text clickCountText;
    public Text mousePowerText;

    private int clickCount;
    private int mousePower = 1;
    private int mousePowerCost = 50;
    private int mousePowerBought = 0;

    // Add this variable
    private StrawberryClicker strawberryClicker;

    void Start()
    {
        // Add these lines to update the click count when the shop is opened
        strawberryClicker = FindObjectOfType<StrawberryClicker>();
        clickCount = strawberryClicker.ClickCount;
        UpdateUI();

        mousePower = PlayerPrefs.GetInt("MousePower", 1);
        mousePowerBought = PlayerPrefs.GetInt("MousePowerBought", 0);

        shopPanel.SetActive(false);

        shopButton.onClick.AddListener(OpenShop);
        closeButton.onClick.AddListener(CloseShop);
        buyMousePowerButton.onClick.AddListener(BuyMousePower);
    }


    // Add this method to update click count when it changes in StrawberryClicker
    public void UpdateClickCount(int newClickCount)
    {
        clickCount = newClickCount;
        UpdateUI();
    }


    void UpdateUI()
    {
        clickCountText.text = $"Clicks: {clickCount}";
        mousePowerText.text = $"Mouse Power: {mousePower} (Bought: {mousePowerBought})";
    }

    public void OpenShop()
    {
        shopPanel.SetActive(true);
        // Stop saving the score when the shop is opened
        FindObjectOfType<StrawberryClicker>().StopSavingScore();
    }

    public void CloseShop()
    {
        // Save the score before closing the shop
        FindObjectOfType<StrawberryClicker>().SaveScore();
        shopPanel.SetActive(false);
        // Resume saving the score when the shop is closed
        FindObjectOfType<StrawberryClicker>().StartSavingScore();
    }

    public void BuyMousePower()
{
    Debug.Log("BuyMousePower() called");
    Debug.Log($"clickCount: {clickCount}, mousePowerCost: {mousePowerCost}");

    if (clickCount >= mousePowerCost)
    {
        Debug.Log("Purchase successful");
        clickCount -= mousePowerCost;
        mousePower++;
        mousePowerBought++;
        // Save the updated score immediately after the purchase
        LocalStorageManager.SaveScore(clickCount);
        // Update the UI with the new score
        UpdateUI();
        // Save the mouse power and mouse power bought values
        PlayerPrefs.SetInt("MousePower", mousePower);
        PlayerPrefs.SetInt("MousePowerBought", mousePowerBought);
    }
    else
    {
        Debug.Log("Insufficient clicks to purchase Mouse Power");
    }
}

}

LocalStorageManager.cs

using UnityEngine;
using System;
using System.Security.Cryptography;
using System.Text;

public class LocalStorageManager : MonoBehaviour
{
    private static string userIdKey = "user_id";
    private static string scoreKey = "score";

    // Define your secret key here
    private static readonly string secretKey = "s3cr3tK3y@123"; // Replace this with a securely generated key

    public static string GetUserId()
    {
        if (!PlayerPrefs.HasKey(userIdKey))
        {
            string newUserId = GenerateUserId();
            PlayerPrefs.SetString(userIdKey, newUserId);
            PlayerPrefs.Save();
            return newUserId;
        }
        else
        {
            return PlayerPrefs.GetString(userIdKey);
        }
    }

    public static void SaveScore(int score)
    {
        string encryptedScore = Encrypt(score.ToString());
        PlayerPrefs.SetString(scoreKey, encryptedScore);
        PlayerPrefs.Save();
        Debug.Log($"Score saved: {score}");
    }

    public static int LoadScore()
    {
        if (PlayerPrefs.HasKey(scoreKey))
        {
            string encryptedScore = PlayerPrefs.GetString(scoreKey);
            string decryptedScore = Decrypt(encryptedScore);
            if (int.TryParse(decryptedScore, out int score))
            {
                Debug.Log($"Score loaded: {score}");
                return score;
            }
        }
        Debug.Log("No score found, returning default score 0");
        return 0; // Default score if not found
    }

    private static string GenerateUserId()
    {
        return Guid.NewGuid().ToString();
    }

    private static string Encrypt(string plainText)
    {
        byte[] data = Encoding.UTF8.GetBytes(plainText);
        using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
        {
            byte[] keys = md5.ComputeHash(Encoding.UTF8.GetBytes(secretKey));
            using (TripleDESCryptoServiceProvider tripDes = new TripleDESCryptoServiceProvider()
            {
                Key = keys,
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            })
            {
                ICryptoTransform transform = tripDes.CreateEncryptor();
                byte[] results = transform.TransformFinalBlock(data, 0, data.Length);
                return Convert.ToBase64String(results, 0, results.Length);
            }
        }
    }

    private static string Decrypt(string cipherText)
    {
        byte[] data = Convert.FromBase64String(cipherText);
        using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
        {
            byte[] keys = md5.ComputeHash(Encoding.UTF8.GetBytes(secretKey));
            using (TripleDESCryptoServiceProvider tripDes = new TripleDESCryptoServiceProvider()
            {
                Key = keys,
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            })
            {
                ICryptoTransform transform = tripDes.CreateDecryptor();
                byte[] results = transform.TransformFinalBlock(data, 0, data.Length);
                return Encoding.UTF8.GetString(results);
            }
        }
    }
}

I tried remaking the ints into public, importing them from the main script (strawberryclicker.cs), asking chatgpt, looking for the answers myself and nothing worked. The expectation of the score being one for all methods failed. That’s why i ask you guys, maybe here I will find some help here.

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