Hello, my problem deals with PlayerPrefs. I’m working on a game for a class and I’m having some trouble with the character lives. The way it’s setup when both characters have fallen of a platform and reach a certain point the game reloads the level. The problem with this is that every time it did this the lives would reset back to 3(there max lives) instead of keeping it lost.
Anyway, I’ve been using PlayerPrefs to make sure that the amount of lives the character has doesn’t reset back to 3 when they die and the game starts over. The problem I’m having though is that I have no idea how to get it to have there lives reset back to 3 both when they have reached 0 lives and have to restart the game or when you quit to the title screen.
Also for some reason when the character hits the Sewage they lose 2 lives instead of 1. Not really sure why.
Here’s the damage script I’m using for this:
using UnityEngine;
using System.Collections;
public class DamageScript : MonoBehaviour
{
GameManager player;
uiScript ui;
// Use this for initialization
void Start ()
{
player = GameObject.FindGameObjectWithTag("GameManager").GetComponent<GameManager>();
ui = GameObject.FindGameObjectWithTag("UI").GetComponent<uiScript>();
}
void OnTriggerEnter(Collider other)
{
if(other.tag == "PlayerOne")
{
Debug.Log("AAARGH");
player.PlayerOne_health -= 1;
if(player.PlayerOne_health <= 2)
{
ui.show1 = true;
}
if(player.PlayerOne_health <= 1)
{
ui.show2 = true;
}
if(player.PlayerOne_health <= 0)
{
player.PlayerOne_health = 0;
ui.show3 = true;
ui.Dead = true;
}
}
if(other.tag == "PlayerTwo")
{
player.PlayerTwo_health += -1;
if(player.PlayerTwo_health <= 0)
{
player.PlayerTwo_health = 0;
ui.Dead = true;
}
}
}
// Update is called once per frame
void Update ()
{
//My attempt to reset the characters lives
PlayerPrefs.SetInt("Player One Lives", player.PlayerOne_health);
PlayerPrefs.SetInt("Player Two Lives", player.PlayerTwo_health);
}
}