I have it so that when my player life hits zero, he dies and so the player health script tells the gm script to take a life away. This isn’t happening, but when the player picks up a coin the gm script still adds coins so I don’t think the gm script is the issue but I cant find anything wrong with my player health script. I know the player is dying because his transform gets reset like playerhealth tells it to.
gm script:
using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class GM : MonoBehaviour {
public int coins = 0;
public int lives = 3;
public Text coinsText;
public Text livesText;
void Start()
{
coinsText.text = coins.ToString();
livesText.text = lives.ToString();
}
void Update()
{
//Game over Screen
if (lives == 0)
{
SceneManager.LoadScene("GameOverScreen");
}
}
public void CoinWasPickedUp(int worth)
{
coins += worth;
coinsText.text = coins.ToString();
}
public void LifeLost()
{
lives -= 1;
livesText.text = lives.ToString();
}
}
playerHealth script:
using System.Collections; using System.Collections.Generic;
using UnityEngine;
public class playerHealth : MonoBehaviour {
public float fullHealth;
float currentHealth;
PlayerController controlMovement;
Vector3 startPosition;
Rigidbody2D rb;
void Start ()
{
currentHealth = fullHealth;
controlMovement = GetComponent<PlayerController>();
startPosition = transform.position;
}
public void addDamage (float damage)
{
if (damage <= 0) return;
currentHealth -= damage;
if (currentHealth <= 0)
{
Die();
}
}
public void Die ()
{
transform.position = startPosition;
rb.velocity = new Vector2();
FindObjectOfType<GM>().LifeLost();
}
}