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

Enemy Health Not Decreasing

Discussion in 'Scripting' started by SleepySalandit, Jul 20, 2022.

  1. SleepySalandit

    SleepySalandit

    Joined:
    Jul 8, 2022
    Posts:
    4
    So I'm trying to make an idle rpg and the enemy health variable is not decreasing, even when the attack is above the enemy's defense. Here is the part of my enemy code I'm trying to use to make this happen:
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System;
    4. using UnityEngine;
    5. using BreakInfinity;
    6. using UnityEngine.UI;

    7. [Serializable]
    8. public class Enemy : MonoBehaviour
    9. {
    10. public Data data;
    11. public GameObject currentEnemy;
    12. public float respawnTime = 4.0f;
    13. public BigDouble enemyLostHealth() => (data.minionAttack -= data.enemyDefense);
    14. public float enemyID;
    15. public float enemyZoneID;
    16. public bool enemyRespawn = true;
    17. public bool damageEnemy = false;
    18. public BigDouble minionAttack;
    19. public BigDouble enemyDefense;
    20. public BigDouble enemyBossDefense;

    21. public BigDouble enemyHealth;


    22. public Text EnemyNameText;
    23. public Text EnemyClassText;
    24. public Text EnemyAttackText;
    25. public Text EnemyDefenseText;
    26. public Text EnemyHealthText;

    27. public static Enemy instance;
    28. private void Awake()
    29. {
    30. instance = this;
    31. }

    32. public void TakeDamage(BigDouble damageAmount)
    33. {
    34. damageAmount = data.minionAttack -= data.enemyDefense;
    35. data.enemyHealth -= damageAmount;

    36. if (data.enemyHealth <= 0)
    37. {
    38. currentEnemy.SetActive(false);
    39. enemyRespawn = true;
    40. }

    41. UpdateBattleUI();

    42. }

      Any ideas?
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    When posting code to the forum, make sure to use Code Tags so it's easier for people to read.

    So many things could be the problem, you're going to have to do some debugging and investigation on your own side. One thing which many people do is use the Debug.Log() method to print out what variables are during runtime, which lets you know what's happening during your code. Make sure the method is actually being called, and that the numbers it's sending are correct.
     
  3. Sphinks

    Sphinks

    Joined:
    Apr 6, 2019
    Posts:
    267
    I think line 40 is not correct. Does this even work ?

    As @RadRedPanda already said, use Debug.Log to check if, all values are correct. I would start to check if "damageAmount" has the correct value. Put this on line 41:
    Debug.Log(damageAmount)


    *edit:Why does yout method "TakeDamage" takes a parameter which you overwrite in the first line of the method ?