- Home /
Lose health on collision
[EDIT]: Yes, the "Is Trigger" is checked.
When the player collides with an enemy, he doesn't lose health. But when he attacks the enemy, enemy loses. And they have almost the same scripts.
Enemy.cs
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
public int currentHealth = 2;
public int maxHealth = 2;
void Start () {
}
void Update () {
if(currentHealth > maxHealth) {
currentHealth = maxHealth;
}
if(currentHealth <= 0) {
currentHealth = 0;
Die();
}
}
void Die() {
Destroy(gameObject);
}
void OnTriggerEnter(Collider col) {
if (col.tag == "Player") {
ScoreAndHealthSystem sh = (Player)ScoreAndHealthSystem.GetComponent("ScoreAndHealthSystem");
sh.currentHealth--;
}
}
}
The problem is probably at "void OnTriggerEnter"
ScoreAndHealthSystem.cs
using UnityEngine;
using System.Collections;
public class ScoreAndHealthSystem: MonoBehaviour {
public int currentScore = 0;
public int maxScore = 100;
public int currentHealth = 3;
public int maxHealth = 99;
public int damage = 1;
void Start () {
}
void Update () {
if(currentScore > maxScore) {
currentScore = maxScore;
}
if(currentScore < 0 ) {
currentScore = 0;
}
if(currentScore == maxScore) {
GiveExtraLife();
}
if(currentHealth < 0) {
currentHealth = 0;
GameOver();
}
if(currentHealth > maxHealth) {
currentHealth = maxHealth;
}
}
void GiveExtraLife() {
currentHealth++;
currentScore = 0;
}
void OnTriggerEnter(Collider col) {
if (col.tag == "Collectible") {
currentScore++;
Destroy(col.gameObject);
}
}
void GameOver() {
}
}
PlayerAttack.cs
using UnityEngine;
using System.Collections;
public class PlayerAttack : MonoBehaviour {
public GameObject enemy;
void Start () {
}
void Update () {
if(Input.GetMouseButtonUp(0)) {
Attack();
}
}
void Attack() {
float dis = Vector3.Distance(enemy.transform.position, transform.position);
Debug.Log(dis);
if(dis <= 1.3) {
Enemy eh = (Enemy)enemy.GetComponent("Enemy");
eh.currentHealth--;
}
}
}
Is the "Is Trigger" box checked in the inspector for the collider?
Add another collider on player character with "Is Trigger" checked or move player health code into "OnCollisionEnter" function.
Yes it is. And if I'll move the collision part, it will cause an error, because "OnCollisionEnter" is already used. I did try that before.
You can have more than one functionality inside "OnCollisionEnter" event function...
Answer by Eltore · Dec 27, 2012 at 01:54 PM
Solved. I'm the stupidiest person ever. I realized, why I got that error. It's because I did it like this:
OnTriggerEnter(Collider col) {
if(col.tag == "Example") {
DoSomething()
}
}
OnTriggerEnter(Collider coll) {
if(coll.tag == "Example_two") {
DoSomething()
}
}
Instead of this:
OnTriggerEnter(Collider col) {
if(col.tag == "Example") {
DoSomething()
}
if(col.tag == "Example_two") {
DoSomething()
}
}
Your answer
Follow this Question
Related Questions
Enemy not taking damage on collisions. 2 Answers
Decreasing Player Health On Collision with Enemy 2 Answers
Fist Punch Collision 0 Answers
Enemy Collision Wont Work! PLEASE HELP 0 Answers