Search Unity

Damage Aply Problem

Discussion in 'Getting Started' started by boazroskam_unity, Nov 27, 2019.

  1. boazroskam_unity

    boazroskam_unity

    Joined:
    Nov 17, 2019
    Posts:
    1
    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:

    Code (CSharp):
    1.  public int Phealth = 100;
    2.     public GameObject KillEffect;
    3.  
    4.     public void PTakeDamage(int Pdamage)
    5.     {
    6.         Phealth = Phealth - Pdamage;
    7.  
    8.         if (Phealth <= 0)
    9.         {
    10.             PlayerDie();
    11.         }
    12.     }
    13.  
    14.  
    15.     void PlayerDie()
    16.     {
    17.         //Instantiate(KillEffect, transform.position, Quaternion.identity);
    18.         Destroy(this.gameObject);
    19.     }
    Code (CSharp):
    1. {
    2.  
    3.  
    4.  
    5.  
    6. Enemy Code:
    7.  
    8.  
    9.   public int health = 100;
    10.     public GameObject deathEffect;
    11.     public GameObject ExplodeEffect;
    12.     private Transform Player;
    13.     public Player player;
    14.     public int Pdamage = 1;
    15.  
    16.  
    17.     void Start()
    18.     {
    19.        Player = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();  
    20.     }
    21.  
    22.  
    23.     void Update()
    24.     {
    25.         if (this.gameObject.transform.position == Player.transform.position)
    26.         {
    27.             Explode();
    28.         }
    29.  
    30.     }
    31.  
    32.  
    33.         void Explode()
    34.         {
    35.         //Instantiate(ExplodeEffect, transform.position, Quaternion.identity);
    36.         player.PTakeDamage(Pdamage);
    37.         Destroy(gameObject);
    38.     }
    39.  
    40.     }
     
  2. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    6,179
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,334
    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.