How can I move the coins collected in one scene to a coin stockpile saved in playerprefs in another scene?

What I am trying to do is collect coins in my main scene and then when I switch to my “safeRoom” scene the coins subtract from my main coin int and add to my new coinPile int that is saved in playerprefs. Kind of like taking the money in your pocket and putting it in a bank.

I’m using DontDestroyOnLoad to keep the collected coins when entering a new scene and then added this code to my GUI to make the coins move to the new int…

#pragma strict
 var CoinPile : int;
 var coinCount : GUIText;
function Start () {

CoinPile = PlayerPrefs.GetInt("coinsCounted");
 


}

function Update () {


 if (PlayerPrefs.GetInt("coinsCounted")<CoinPile)
        {
            PlayerPrefs.SetInt("coinsCounted", CoinPile);
        }
       
}

function OnGUI ()
{
      coinCount.text = "Coins Collected: " + CoinPile;
   
}

but obviously I’m doing it wrong. Can anyone help?

Why don’t you just save the coin count to PlayerPrefs, then read it from PlayerPrefs when entering the other scene? You wouldn’t need to use DontDestroyOnLoad for that.