I having an issue where when my player hit an enemy he dies as he should, but sometimes he loses 2 lives and others he only loses 1. I think the issue might be that my character has two colliders: a circle collider2D and a box Collider2D and they are both hitting the enemy. I have no idea how to fix it though. Here are my scripts (They are both attached to my player)
Lives:
using UnityEngine;
using System.Collections;
public class Lives : MonoBehaviour {
public static int PlayerLives = 5;
public int MaxLives = 999;
public GUISkin Life;
public Rect LifeDisplay;
void Update ()
{
if(PlayerLives >= MaxLives)
{
PlayerLives = MaxLives;
}
if(PlayerLives <= 0)
{
Application.LoadLevel(0); //Gameover Level
}
PlayerPrefs.SetInt("Lives", PlayerLives);
PlayerLives = PlayerPrefs.GetInt("Lives");
}
void OnGUI()
{
GUI.skin = Life;
GUI.Label(LifeDisplay, "Lives: " + PlayerLives.ToString(), GUI.skin.GetStyle("Lives"));
}
}
Enemy:
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
public string Level = "";
void OnTriggerEnter2D(Collider2D enemy)
{
if(enemy.tag == "Enemy")
{
Lives.PlayerLives--;
Application.LoadLevel(Level);
}
}
}
Any help would be much appreciated.
↧