- Home /
This collision Enemy health code suddenly stopped working.
I can't understand why my code isn't working. I used this the other day for an hour and it worked perfectly. Then suddenly it just stopped working! The float values will not subtract whatsoever. The collision is working, and the tag system as well.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHealthSystem : MonoBehaviour
{
public float HP = 100;
public float PistolDamage = 5;
public GameObject DestroyMe;
void Damager (float amount)
{
HP -= amount;
Debug.Log ("amount" + amount);
}
void OnCollisionEnter (Collision col)
{
if (col.gameObject.tag == (" PistolAmmo "))
Damager (PistolDamage);
{
Debug.Log ("HP"+ HP);
}
}
void Update()
{
if (HP <= 0)
Destroy (DestroyMe);
}
}
Answer by ToTheStarsS2 · Apr 24, 2018 at 12:53 PM
I'm a noob at coding but i figured out a fix for those interested,
if I called it before I used the method, and put the method in separate brackets everything seemed to work fine!
void OnCollisionEnter (Collision col) { if (col.gameObject.tag == (" PistolAmmo ")) HP -= PistolDamage; { TakeDamage (PistolDamage); }
Your answer
Follow this Question
Related Questions
weapon collision with characters 1 Answer
Character controller mess up damage 3 Answers
Player take damage on collision with AI 1 Answer
Velocity data from OnCollisionEnter is delayed (incorrect) 3 Answers
Take health from enemy 3 Answers