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.