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

Hi guys Please help my player is shooting but no killing

Discussion in '2D' started by milda120, Jan 11, 2021.

  1. milda120

    milda120

    Joined:
    Dec 19, 2020
    Posts:
    9
    This is the script and i am new so i am teaching upload_2021-1-11_16-13-34.png
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    So you have declared the function TakeDamage but you need to call it when a projectile or something hits the enemy. Basically, you have declared a function but have not called it yet. When you are not using a built in function like Start, Update, OnTriggerEnter2D, etc. you always need to call it so it occurs.

    For a basic solution, create an OnTriggerEnter2D in your enemy script and when a projectile hits that trigger, it will call the TakeDamage function.

    Something like this:
    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D other)
    2. {
    3.     if(other.gameObject.tag == "Projectile")
    4.     {
    5.         TakeDamage(25);
    6.         Debug.Log("We have hit the enemy with a projectile");
    7.     }
    8. }
    Make sure both the Enemy gameobject and projectile (bullet, fireball, etc.) have a Collider2D object and make the Projectile's collider be set as a trigger and change the tag to Projectile. Test it from there.
     
    Vryken likes this.
  3. milda120

    milda120

    Joined:
    Dec 19, 2020
    Posts:
    9
    Thanks you so much :)