Helath and Damage Player

Hi everyone
I did this script for my character gets damage when he is touched by enemy(Zombie) but his Health
does not decrease in this case could u help me?
I´m learning how to programm and i´m really novice so… :wink:

Here is my code

Sorry i do not know how to add the code like u , getting color and that kind stuff

using UnityEngine;
using System.Collections;

public class HealthGaby : MonoBehaviour
{

public float health = 100;
public const float maxHealth = 100;
public Texture2D gabyH = null;
public float modifySize = 0.86f;
public Rect postion = new Rect(5, 5, 100, 25);

private void Start()

{
health = maxHealth;

}

private void Update()

{
if (health < 0)
{
health = 0;
}

else if (health > maxHealth)
{
health = maxHealth;

}

}

private void OnGUI()

{
if (gabyH != null)
{
postion.width = health*modifySize;
GUI.DrawTexture(postion, gabyH);
}
}

void OnCollisionEnter(Collision col)

{
if (col.gameObject.tag == “Enemy”)
{
DamagePlayer(5);
Destroy(col.other);
}
}

public void DamagePlayer(float damage)

{
health -= damage;
}

}

To add code tags: Using code tags properly - Unity Engine - Unity Discussions

You can type [ code ] and [ /code ] without the spaces.

I do not see an immediate problem with your script. There could be a number of problems with your scene setup though. Try placing Debug.Log statements in your functions. To start with, place one at the start of your OnCollisionEnter statement to ensure that you are registering collisions. You may not have colliders/rigidbodies properly set up.

Also, make sure that the enemy is tagged properly.

I would recommend that you use OnTriggerEnter(Collider Other)

And make a sphere collider on the SAME object as the script and then set the sphere collider to Trigger

This allows for a little bit of range because you might not want to have to zombie or whatever enemy right on top of you

Yes he does under the OnCollisionEnter he does a call for DamagePlayer(5)

:frowning: stil now it does not work . But thx guys for your Advices;)

What object has the script on it?