I'm doing a script to take life out of the player when something collides with it, I've reviewed the code dozens of times and can not find the error.
The collision code is tied to enemies:
PlayerHealth ph;
public int danoAtaque = 10;
void OnTriggerEnter (Collider other)
{
ph = gameObject.AddComponent<PlayerHealth> ();
if (other.tag == "Player")
{
Debug.Log ("Entrou no Player");
ph.TakeDamage(danoAtaque);
Debug.Log ("Tirou vida");
if (ph.vidaAtual <= 0)
{
Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
gameController.GameOver ();
Destroy (other.gameObject);
Debug.Log ("Morreu");
}
}
if (other.tag == "Boundary" || other.tag == "Enemy") {
return;
}
if (explosion != null) {
Instantiate (explosion, transform.position, transform.rotation);
}
gameController.AddScore(scoreValue);
Destroy (gameObject);
}
This other code is what is in the Player:
static int vidaInicial = 100;
public int vidaAtual;
public Slider healthSlider;
public Image damageImage;
public float flashSpeed = 5f;
public Color flashColour = new Color(1f, 0f, 0f, 0.1f);
void Awake ()
{
vidaAtual = vidaInicial;
}
public void TakeDamage (int amount)
{
Debug.Log("Chamou Funçao");
vidaAtual -= amount;
Debug.Log("Deu Dano");
}
void OnGUI()
{
GUI.Label (new Rect (10, 10, 100, 20), "Vida: " + vidaAtual);
}