2
I’m making a script for take the player’s life when something collides with it, I’ve reviewed the code dozens of times and can’t find the error.
The collision code is tied to the 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 the one in 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);
}
Rafa Arruda, I edited your question, I removed some tags and changed the title, if you want, you can reverse it. Or edit it.
– stderr