PlayerPrefs, First time applying and need help.

Hello community.

I want to save lives after the level is restarted by pressing a button, I’ve applied PlayerPrefs but it do nothing on the practice.

Here I’ll copy my scripts.

using UnityEngine;
using System.Collections;

public class VidaScript : MonoBehaviour {

	//Interfaz Grafica Vida
	
	public int vida = 2;
	public GUIText LivesText;

	//Prueba

	public static string Vidas;



	void Start () {

		UpdateVida ();
	}
	

	public void Subtract (int newValueVida)
	{
		vida -= newValueVida;
		UpdateVida ();
		PlayerPrefs.SetInt (Vidas, newValueVida);

	}
	void UpdateVida ()
	{
		
		LivesText.text = "Lives: " + vida;
	}
}

And the other script.

using UnityEngine;
using System.Collections;

/// Script despues de morir

public class GameOverScript : MonoBehaviour
{

private VidaScript vidaScript;
private int lives;

void Start ()
{
			GameObject vidaScriptObject = GameObject.FindGameObjectWithTag ("VidaScript");
			if (vidaScriptObject != null) {
					vidaScript = vidaScriptObject.GetComponent <VidaScript> ();
			}
			if (vidaScript == null) {
					Debug.Log ("No se puede encontrar VidaScript");
			}	

	}

void OnGUI()
{
	const int anchoBoton = 140;
	const int altoBoton = 60;
	
	if (
		GUI.Button(
		new Rect(
		Screen.width / 2 - (anchoBoton / 2),
		(1 * Screen.height / 3) - (altoBoton / 2),
		anchoBoton,
		altoBoton
		),
		"Restart level"
		)
		)
	{
		// Reiniciamos el nivel y no destruimos.
		Application.LoadLevel(Application.loadedLevel);
		PlayerPrefs.GetInt(VidaScript.Vidas);
	}
	
	if (
		GUI.Button(
		new Rect(
		Screen.width / 2 - (anchoBoton / 2),
		(2 * Screen.height / 3) - (altoBoton / 2),
		anchoBoton,
		altoBoton
		),
		"Back to MainScreen"
		)
		)
	{
		// Vamos a Menu
		Application.LoadLevel("StartScene");
	}
}

}

If somebody can explain to me, why it don’t work and how it works, I’ll really appreciate it.

Thanks a lot for your help.

Change this:

PlayerPrefs.SetInt (Vidas, newValueVida);

to be this:

PlayerPrefs.SetInt (Vidas, vida);

And this:

PlayerPrefs.GetInt(VidaScript.Vidas);

should instead be this:

vidaScript.vida = PlayerPrefs.GetInt(VidaScript.Vidas);

That should do it.

I’ve done it in a diferrent way. And it works perfect! Thanks for the help, Steel_Arm you guide me in the way.

Here’s my code.

using UnityEngine;
using System.Collections;

public class VidaScript : MonoBehaviour {

	//Interfaz Grafica Vida
	
	public int vida = 0;
	private static int vidasIniciales = 2;
	private static int vidasActuales;
	public GUIText LivesText;

	//Prueba


	static VidaScript ()
	{
		vidasActuales = PlayerPrefs.GetInt("lives", vidasIniciales);
	}

	public static int VidasActuales {
				get { return vidasActuales; }
				set {
						vidasActuales = Mathf.Max (0, value); // Para que no de negativo.
						PlayerPrefs.SetInt ("lives", vidasActuales);
				}
		}

	void Start () 
	
	{
		vidasActuales = PlayerPrefs.GetInt("lives", vidasIniciales);
		UpdateVida ();

	}



	public void Subtract (int newValueVida)
	{
		vida -= newValueVida;
		UpdateVida ();


	}
	void UpdateVida ()
	{
		
		LivesText.text = "Lives: " + vidasActuales.ToString ();
	}

	public void drecrementoVidas ()
	{
				vidasActuales--;
		PlayerPrefs.SetInt("lives", vidasActuales);
		}
	public void incrementoVidas ()
	{
		        vidasActuales++;
		PlayerPrefs.SetInt("lives", vidasActuales);

		}
	public void zeroVidas()
	{
		if (VidaScript.vidasActuales > 0)
			Application.LoadLevel("StartScene");
	}
}