Saving coins with C#

Hello Im making a 2d runner and Im at the part where I want to save my collected coins.
Here is my coin script that I currently have:

using UnityEngine;

public class Coin: MonoBehaviour {

	public Vector3 offset, rotationVelocity;
	public float recycleOffset, spawnChance;
	
	void Update () {
		if(transform.localPosition.x + recycleOffset < Runner.distanceTraveled){
			gameObject.active = false;
			return;
		}
		transform.Rotate(rotationVelocity * Time.deltaTime);
	}
	
	void OnTriggerEnter () {
		Runner.AddCoin();
		gameObject.active = false;
	}
	
	public void SpawnIfAvailable (Vector3 position) {
		if(gameObject.active || spawnChance <= Random.Range(0f, 100f)) {
			return;
		}
		transform.localPosition = position + offset;
		gameObject.active = true;
	}
	
	private void GameOver () {
		gameObject.active = false;
	}
}

And the coin scripting is in the runner script also:

using UnityEngine;
using System.Collections;
 
public class Runner : MonoBehaviour {
 
    public static float distanceTraveled;
 
    public float acceleration;
    public Vector3 boostVelocity, jumpVelocity;
    public float gameOverY;
 
    private bool touchingPlatform;
	
	private static int boosts;
	private static int coins;
 
    void Update () {
       if(Input.GetButtonDown("Jump")){
			if(touchingPlatform){
         rigidbody.AddForce(jumpVelocity, ForceMode.VelocityChange);
         touchingPlatform = true;
				}
			else if(boosts > 0){
				rigidbody.AddForce(boostVelocity, ForceMode.VelocityChange);
				boosts -= 1;
				GUIManager.SetBoosts(boosts);
			}
       }
       distanceTraveled = transform.localPosition.x;
		GUIManager.SetDistance(distanceTraveled);
 
       if(transform.localPosition.y < gameOverY)
	StartCoroutine("DelayedLevelLoad");
	}
				
			IEnumerator DelayedLevelLoad(){
          yield return new WaitForSeconds(1);
          Application.LoadLevel("GameOver");
         }
			
 
    void FixedUpdate () {
       if(touchingPlatform){
         rigidbody.AddForce(acceleration, 0f, 0f, ForceMode.Acceleration);
       }
    }
 
    void OnCollisionEnter () {
       touchingPlatform = true;
    }
 
    void OnCollisionExit () {
       touchingPlatform = false;
    }
	
	private void GameStart () {
		boosts = 0;
		coins = 0;
		GUIManager.SetBoosts(boosts);
		GUIManager.SetCoins(coins);
		distanceTraveled = 0f;
		GUIManager.SetDistance(distanceTraveled);
		
	}
	
	public static void AddBoost(){
		boosts += 1;
		GUIManager.SetBoosts(boosts);
	}
	
	public static void AddCoin(){
		coins += 1;
		GUIManager.SetCoins(coins);
	}
}

I want atleast something simple like showing my collected coins in this run on the game over scene and showing the total amount of collected coins on the main menu scene. Ive tried looking at playerprefs but all that I found was unityscript. Im still pretty new so please help.

Thanks.

Unless you have a specific reason to use 3d text, don’t. It also seems like you are solving this in a much more complex manner than necessary which is not a bad thing, it’s all part of the learning process.

I could be wrong as I am somewhat of a novice myself still. But I would just declare your coin variable as public static. Then you can add to it simply by doing something like (and easily access it from any script):

if (your coin pickup condition)
{
   scriptName.publicStaticCoinVariableName += 1;

}

The first way I learned how to display text variables on screen was to do this in Unity;

Game Object > Create Other > GUI Text;

Name your new GUI Text you created “GUI Coins” in your Hierarchy.

Then in any of your scripts (it shouldn’t matter as long as they are in the scene and the object they are attached to won’t be destroyed)… do something like this.

//declare variable

GUIText GUICoins;

void Start()
{
    GUICoins = GameObject.Find("GUI Coins").guiText;
    GUICoins.text = ""+ coinVariable;

}

void Update()
{
    GUICoins.text = ""+ coinVariable;

}

And of course you would adjust the coin Variable to what you are actually using currently.

Then you can reposition your GUI Text in the Game View in Unity to wherever you want it on screen.

You do not need to do so many fancy things for a problem like this, although it’s a great learning experience. You can simply set your coin variable to be public static, and then display it and update it as described above.

If you really need 3d text I believe a very similar process should work, just use 3D text instead of GUI Text when you create your new object. And make sure you don’t destroy your script containing the coin variable if you’re doing this in a new scene.

You could use PlayerPrefs in your GUIManager script: read coins at Start, and save it in SetCoin - something like this (GUIManager script):

// read PlayerPrefs "Coins" value at Start:
void Start(){
    coins = PlayerPrefs.GetInt("Coins", 0);
    ...

// update PlayerPrefs "Coins" whenever SetCoins is called:
void SetCoins(int coins){ 
    PlayerPrefs.SetInt("Coins", coins);
    ...