health and damage

hi guys, i have this code for health but i dont know how to make my player take damage, i tried taking codes from other people but their codes mess up almost every thing. any tips or videos that you know of can help?

health code:

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

public class Player : MonoBehaviour
{

    public int maxHealth = 100;
    public int currentHealth;

    public HealthBar healthBar;

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

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

    }

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

        healthBar.SetHealth(currentHealth);

        if(currentHealth <= 0)
        {

        }
        void die()
        {
           
        }
    }
}

and this is the health bar code:

using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.UI;

public class HealthBar : MonoBehaviour
{

    public Slider slider;
    public Gradient gradient;
    public Image fill;

    public void setMaxHealth(int health)
    {
        slider.maxValue = health;
        slider.value = health;

        fill.color = gradient.Evaluate(1f);
    }

    public void SetHealth(int health)
    {
        slider.value = health;

        fill.color = gradient.Evaluate(slider.normalizedValue);
    }
}

You literally have a method for TakeDamage(). I take it this is not your code but something you copied from some tutorial. Which isnt a bad thing, but you may want to learn to understand coding yourself.

Anyways, when you want your player object to take damage, you need a reference to that object. This is usually easy, as damage is often taken from collisions or raycasts, so the reference to the hit object is known. Then get the reference to this player script, on which you then simply call the TakeDamage() method and pass the value for how much damage the player should take.

So let’s say you take damage from a collision. That would look something like this:

int myDamage = 10;

void OnCollisionEnter(Collision collision)
{
    GameObject other = collision.gameObject;
    Player playerScript = other.GetComponent<Player>();
    if(playerScipt != null){ // <-- in this case we collided with the player
        playerScript.TakeDamage(myDamage);
    }
}

The above code assumes the other object makes the player take damage (ie, the code is inside the enemy script or whatever makes the player take damage, in this example on collision). We could also make the player check for collisions themselves and ask the other object how much damage it does, which is probably what i’d do, but given what you posted this seemed easier to understand.

2 Likes

thank you so much for this, also yea im new so i dont maake the full code myself so i try to copy some code from youtube, but im getting better a bit now

This is a great approach… but before you try changing anything, get a fully functional tutorial (i.e., one that comes with all the code in a download, fully functional), get it all working, and then:

THIS is the most important part: step through ALL of it, until you understand 100% of how it works.

Generally with software engineering, guessing in a vacuum will not be useful, as it is engineering.

2 Likes