but didn’t work, when playing same level in multiple times didn’t save collected coin amount.
Here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CoinMain : MonoBehaviour
{
Text coinMainText;
public static int coinMainAmount =0;
public static CoinMain instance; // Singleton instance
void Awake()
{
// Ensure only one instance of CoinManager exists
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
void Start()
{
}
// Update is called once per frame
void Update()
{
UpdateStarUI();
PlayerPrefs.SetInt("MCAw", coinMainAmount);
}
private void UpdateStarUI()
{
// Assuming you have a total number of levels stored in a variable like 'totalLevels'
int totalLevels = 45;
for (int i = 0; i <= totalLevels; i++)
{
coinMainAmount += PlayerPrefs.GetInt("MainCoinAmount" + i);
}
coinMainText.text = coinMainAmount.ToString("000000");
}
}