Save the coins to the mainmenu

Hi i am working on a project i have many scripts for coins the follwoing are the scripts
Here is the gameovermanager script i use it that i am using it in the winning scene and the game over scene where all of the coins show that how many you have collected

using UnityEngine;
using UnityEngine.UI;

public class GameOverManager : MonoBehaviour
{
    public Text totalCoinsText;
    public Text totalApplesText;
    public Text totalOrangesText;
    public Text totalGrapesText;
    public Text totalBananasText;

    void Start()
    {
        totalCoinsText.text = "Total Coins: " + LevelManager.totalCoins;
        totalApplesText.text = "Total Apples: " + LevelManager.totalApples;
        totalBananasText.text = "Total Bananas: " + LevelManager.totalBananas;
        totalGrapesText.text = "Total Grapes: " + LevelManager.totalGrapes;
        totalOrangesText.text = "Total Oranges: " + LevelManager.totalOranges;
    }
}

and the following is the gamemanager script that is i have given in everyscene i mean that in levels and also in the gameover scene

using UnityEngine;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance { get; private set; }

    public int totalCoins;
    public int totalApples;
    public int totalOranges;
    public int totalGrapes;
    public int totalBananas;

    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }
}

And the following is the levelmanger script

using UnityEngine;
using UnityEngine.SceneManagement;

public static class LevelManager
{
    public static string currentLevel;
    public static int totalCoins;
    public static int totalApples;
    public static int totalGrapes;
    public static int totalOranges;
    public static int totalBananas;

    public static void LoadLevel(string levelName)
    {
        currentLevel = levelName;
        //totalCoins = 0;
        SceneManager.LoadScene(levelName);
    }

    public static void ReloadCurrentLevel()
    {
        SceneManager.LoadScene(currentLevel);
    }

    public static void GameOver()
    {
        SceneManager.LoadScene("GameOver");
    }
}

so how can i add the coins to the mainmenu that all of the coins save there and also if i will stop the game then they have to be saved

Use PlayerPrefs to save coins between scenes:
PlayerPrefs.SetInt("COINS_kEY", 50);

and when you initialize get coins from player prefs:
var coins = PlayerPrefs.GetInt("COINS_KEY", 0);

1 Like

In which script can you give me that full script that there i will use playerprefs but in my scriipts

Start with tutorials. Scripts are only a teeny tiny part of the problem. Setup and configuration are far more important.

If you can’t get it from a tutorial you won’t get it from this little text box.

Two steps to tutorials and / or example code:

  1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
  2. stop and understand each step to understand what is going on.

If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.

Try this approach, one step at a time:

Imphenzia: How Did I Learn To Make Games:

No i mean that i have 3 scripts in which script i write playerpref