Pls help with code

I’m trying to make it so that when i touch a zombie, I take damage.Why no work

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class damage2 : MonoBehaviour
{
public int maxHealth = 100; public int currentHealth;

public HealthBar healthBar;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
}
// Update is called once per frame
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag.Equals(“zombie”))
{
TakeDamage(10);
}
void TakeDamage(int damage)
{
currentHealth -= damage;
healthBar.SetHealth(currentHealth);
}
}
}

Try,

currentHealth = currentHealth - damage;

Let me know if it worked.

were in the code?

Replace currentHealth -= damage;

I have a health bar and the health bar isn’t moving

This works

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class damage : MonoBehaviour
{
public int maxHealth = 100;
public int currentHealth;

public HealthBar healthBar;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
}

// Update is called once per frame
private void Update()
{

if (Input.GetKeyDown(KeyCode.Space))
{
TakeDamage(10);
}

void TakeDamage(int damage)
{
currentHealth -= damage;

healthBar.SetHealth(currentHealth);
}
}
}

But thats not what i want

I already have a bar

You want the health bar to move?

Like follow your character or ?

I already have all that. i want to take damage when i touch a thing with the tag “zombie”

Closed.
Post in the correct forum.
Combine your posts, this is not a chat.
USE CODE TAGS
read the rules.

1 Like