Accumulate coins using variables (in-app purchases)

Can anyone help me with the coin calculation.
After completing the level I run this code to add the coins to total coins.
Totalcoins is used for IAP purposes.
However everytime i retry the level the it keeps resetting the total coins value.
Thanks in advance for the solution.

if (coins > 0)
{ 
	   	   totalcoins += coins;
		   PlayerPrefs.SetInt("Total Coins", totalcoins);		   
		   //print(PlayerPrefs.GetInt("Total Coins"));
		   print(totalcoins);
}

totalcoins gets reset because its an instance variable of some script you have that gets destroyed and recreated when the level reloads.

For cross-level persistence, do either of the following: