Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question damage system please help me

Discussion in 'Scripting' started by cagriaskin, Nov 14, 2020.

  1. cagriaskin

    cagriaskin

    Joined:
    Oct 6, 2020
    Posts:
    12
    If you can fix it, can you throw the codes?
    upload_2020-11-14_22-2-26.png

    -----------------------------------------------------------------------------------------------------------------------
    error:

    NullReferenceException: Object reference not set to an instance of an object
    hasar.OnTriggerEnter (UnityEngine.Collider other) (at Assets/hasar.cs:22)

    --------------------------------------------------------------------------------------------------------------
    hasar.cs

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

    public class hasar : MonoBehaviour
    {
    float damage = 20f;
    // Start is called before the first frame update
    void Start()
    {

    }

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

    }

    void OnTriggerEnter(Collider other)
    {
    other.gameObject.GetComponent<Healthbar> ().TakeDamage (damage);
    }
    }

    ----------------------------------------------------------------------------------------------------------------------

    Healthbar.cs

    using System.Collections;
    using UnityEngine;
    using UnityEngine.UI;

    // Only allow this script to be attached to the object with the healthbar slider:
    [RequireComponent(typeof(Slider))]
    public class Healthbar : MonoBehaviour {

    // Visible health bar ui:
    private Slider healthbarDisplay;

    [Header("Main Variables:")]
    // Health variable: (default range: 0-100)
    [Tooltip("Health variable: (default range: 0-100)")] public float health = 100;

    // Percentage of how full your health is: (0-100, no decimals)
    private int healthPercentage = 100;

    // Minimum possible heath:
    [Tooltip("Minimum possible heath: (default is 0)")] public float minimumHealth = 0;

    // Maximum possible health:
    [Tooltip("Maximum possible heath: (default is 100)")] public float maximumHealth = 100;

    // If the character has this health or less, consider them having low health:
    [Tooltip("Low health is less than or equal to this:")] public int lowHealth = 33;

    // If the character has between this health and "low health", consider them having medium health:
    // If they have more than this health, consider them having highHealth:
    [Tooltip("High health is greater than or equal to this:")] public int highHealth = 66;

    [Space]

    [Header("Regeneration:")]
    // If 'regenerateHealth' is checked, character will regenerate health/sec at the rate of 'healthPerSecond':
    public bool regenerateHealth;
    public float healthPerSecond;

    [Space]

    [Header("Healthbar Colors:")]
    public Color highHealthColor = new Color(0.35f, 1f, 0.35f);
    public Color mediumHealthColor = new Color(0.9450285f, 1f, 0.4481132f);
    public Color lowHealthColor = new Color(1f, 0.259434f, 0.259434f);

    private void Start()
    {
    // If the healthbar hasn't already been assigned, then automatically assign it.
    if (healthbarDisplay == null)
    {
    healthbarDisplay = GetComponent<Slider>();
    }

    // Set the minimum and maximum health on the healthbar to be equal to the 'minimumHealth' and 'maximumHealth' variables:
    healthbarDisplay.minValue = minimumHealth;
    healthbarDisplay.maxValue = maximumHealth;

    // Change the starting visible health to be equal to the variable:
    UpdateHealth();
    }

    // Every frame:
    private void Update()
    {
    healthPercentage = int.Parse((Mathf.Round(maximumHealth * (health / 100f))).ToString());

    // If the player's health is below the minimum health, then set it to the minimum health:
    if (health < minimumHealth)
    {
    health = minimumHealth;
    }

    // If the player's health is above the maximum health, then set it to the maximum health:
    if (health > maximumHealth)
    {
    health = maximumHealth;
    }

    // If the character's health is not full and the health regeneration button is ticked, regenerate health/sec at the rate of 'healthPerSecond':
    if (health < maximumHealth && regenerateHealth)
    {
    health += healthPerSecond * Time.deltaTime;

    // Each time the health is changed, update it visibly:
    UpdateHealth();
    }
    }

    // Set the health bar to display the same health value as the health variable:
    public void UpdateHealth()
    {
    // Change the health bar color acording to how much health the player has:
    if (healthPercentage <= lowHealth && health >= minimumHealth && transform.Find("Bar").GetComponent<Image>().color != lowHealthColor)
    {
    ChangeHealthbarColor(lowHealthColor);
    }
    else if (healthPercentage <= highHealth && health > lowHealth)
    {
    float lerpedColorValue = (float.Parse(healthPercentage.ToString()) - 25) / 41;
    ChangeHealthbarColor(Color.Lerp(lowHealthColor, mediumHealthColor, lerpedColorValue));
    }
    else if (healthPercentage > highHealth && health <= maximumHealth)
    {
    float lerpedColorValue = (float.Parse(healthPercentage.ToString()) - 67) / 33;
    ChangeHealthbarColor(Color.Lerp(mediumHealthColor, highHealthColor, lerpedColorValue));
    }

    healthbarDisplay.value = health;
    }

    public void GainHealth(float amount)
    {
    // Add 'amount' hitpoints, then update the characters health:
    health += amount;
    UpdateHealth();
    }

    public void TakeDamage(float amount)
    {
    // Remove 'amount' hitpoints, then update the characters health:
    health -= float.Parse(amount.ToString());
    UpdateHealth();
    }

    public void ChangeHealthbarColor(Color colorToChangeTo)
    {
    transform.Find("Bar").GetComponent<Image>().color = colorToChangeTo;
    }

    public void ToggleRegeneration()
    {
    regenerateHealth = !regenerateHealth;
    }

    public void SetHealth(float value)
    {
    health = value;
    UpdateHealth();
    }
    }
     
    Last edited: Nov 14, 2020
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Please use code tags when posting code...

    "If you can fix it, can you throw the codes?"

    Fix what? You forgot to mention what your problem is.

    And "please help me" isn't very informative either. So this has something to do with damage system, but could you be a little bit more specific?
     
  3. dylanfries

    dylanfries

    Joined:
    Jul 11, 2012
    Posts:
    16
    NullReferenceException: Object reference not set to an instance of an object
    hasar.OnTriggerEnter (UnityEngine.Collider other) (at Assets/hasar.cs:22)

    This is telling you the issue, you have a null object on line 22. Since your OnTrigger only has one line of code, lets start there.

    other.gameObject.GetComponent<Healthbar> ().TakeDamage (damage);

    Now if an object in this line is null, what could it be?
    1. other is null
    2. GetComponent doesnt find a component (perhaps you hit something else that doesn't have a HealthBar?)

    Thats really it. My guess would be that you are not finding a healthbar object on your hit target.

    Try

    HealthBar bar = other.gameObject.GetComponent<HealthBar>();
    if( bar != null){
    bar.TakeDamage(damage);
    }

    Try to read the error messages, they are telling you what is wrong (from Unity's perspective) and are your best tool for troubleshooting.