health script not working
it is supposed to make the health go down if it collides with the knives bt it doesnt work. Attached to the player
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour {
public static int health = 50;
public GameObject Player;
public Slider healthBar;
// Use this for initialization
void Start () {
InvokeRepeating("OnTriggerEnter",1,1);
}
// Update is called once per frame
void OnTriggerEnter(Collider col)
{
if (col.gameObject.name == "knife (1)" ) {
health = health - 50;
healthBar.value = health;
if (health <= 0) {
Destroy (col.gameObject);
Destroy (gameObject);
}
{
if (col.gameObject.name == "knife (2)" ) {
health = health - 50;
healthBar.value = health;
if (health <= 0) {
Destroy (col.gameObject);
Destroy (gameObject);
}
}
}
} }
are you instantiating the knife?
if so, add (Clone) to the name..
You can also use tag ins$$anonymous$$d of checking the name of the object.
if(col.gameObject.tag == "projectile") deduct health...
or add a script to the knife that checks if it collides with a object with tag "Player"..
example.
$$anonymous$$nifeScript.cs
public class $$anonymous$$nifeScript:$$anonymous$$onoBehaviour{
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "Player") {
if(col.gameObject.GetComponent<PlayerHealth>() != null){
col.gameObject.GetComponent<PlayerHealth>() .health -= 50;
col.gameObject.GetComponent<PlayerHealth>() .healthBar.value = health;
if (col.gameObject.GetComponent<PlayerHealth>().health <= 0) {
Destroy (col.gameObject);
Destroy (gameObject);
}
}
}
}
Your answer
Follow this Question
Related Questions
i try make enemy health slider or any uI just show the health of Enemy Over head 0 Answers
How to make my gun do damge and destroy a object? 1 Answer
can't change float value from OnTriggerEnter 1 Answer
dont know why my script is causing unity to freeze. 1 Answer
How to make health and damage script function correctly? 3 Answers