Lose HP with collision.

Hello. I don’t know why my script isn’t working, when my character hits the collider the HP on the Inspector while in game isn’t changing. This is the script:

using UnityEngine;
using System.Collections;

public class HPScript2 : MonoBehaviour {
	
	public int HP = 100;
	public Transform target;
	public GUIText healthText;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		healthText.text = System.Convert.ToString (HP);
	}
	void HPLose(Collider col)
	{
		if(col.tag == "trap")
		{
			HP -= 10;
		}
	}
}

I apply this script to my player controller. Thank you.

1 Answer

1

are you ever calling the HPLose function? I think you may be looking for the OnCollisionEnter(Collision col) method for what you’re doing unless you are calling the HPLose function somewhere else you aren’t showing.

Or OnTriggerEnter, with a trigger collider