Damage Aply Problem

Hallo everyone
i am a beginner and i have been doing unity for just 2 weeks,.
i am trying to lat the enemy’s apply damage to my character.
i just doesnt work, but i dont know why, the void Explode is called when the enemy has the same position as thr character. it just doenst apply damage. can anyone help me?

Player Code:

 public int Phealth = 100;
    public GameObject KillEffect;

    public void PTakeDamage(int Pdamage)
    {
        Phealth = Phealth - Pdamage;

        if (Phealth <= 0)
        {
            PlayerDie();
        }
    }


    void PlayerDie()
    {
        //Instantiate(KillEffect, transform.position, Quaternion.identity);
        Destroy(this.gameObject);
    }
{
 



Enemy Code:

 
  public int health = 100;
    public GameObject deathEffect;
    public GameObject ExplodeEffect;
    private Transform Player;
    public Player player;
    public int Pdamage = 1;


    void Start()
    {
       Player = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();  
    }


    void Update()
    {
        if (this.gameObject.transform.position == Player.transform.position)
        {
            Explode();
        }

    }


        void Explode()
        {
        //Instantiate(ExplodeEffect, transform.position, Quaternion.identity);
        player.PTakeDamage(Pdamage);
        Destroy(gameObject);
    }

    }

This should help, Debug.Log is your friend! Tips for new Unity users

1 Like

Also, it’s very confusing that you have variables called Player and player. Player is a Transform, and you find it on line 19. But player is a Player (the class), and you never assign to it… maybe it’s assigned in the Inspector, but it may have nothing at all to do with the Player transform. I suggest simplifying all that; only get one reference to the player, so you can be sure what you’ve got.