Enemy take damage

I’m trying to enemy take damage which is set on Shooting script.

Enemy Script:

public class SimpleAI : MonoBehaviour
{

    [SerializeField] Transform target;
    NavMeshAgent agent;
    public GameObject player;
    private Shooting shootingScript;


    // Start is called before the first frame update
    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        agent.updateRotation = false;
    }

    // Update is called once per frame
    void Update()
    {
        agent.SetDestination(target.position);
        if(Health == 0)
        {
            Destroy(gameObject);
        }
    }

    public void PlayerDamage()
    {
        gameObject player = gameObject.Find("Player");
        shootingScript = player.GetComponent<Shooting>();
    }

    //Health
    public int Health = 5;

    private void OnTriggerEnter2D(Collider2D other)
    {
        Health -= player.damage;
    }

}

Shooting script:

public class Shooting : MonoBehaviour
{
    public Transform firepoint;
    public GameObject bulletPrefab;
    public float fireRate = 0.5F;
    private float nextFire = 1.5F;
    public int maxAmmo = 10;
    private bool isReloading = false;
    private int currentAmmo;
    public float bulletForce = 20f;
    public float reloadTime = 1f;
    public Text ammoUI;
    public GameObject reloading;
    public GameObject ammoUi;
    public int damage = 1;

    // Update is called once per frame
    void Start()
    {
        ammoUi.SetActive(true);
    }
    void Update()
    {
        ammoUI.text = currentAmmo.ToString();

        if (isReloading)
        {
            return;
        }
        if(currentAmmo <= 0)
        {
            StartCoroutine(Reload());
            return;
        }
        if(Input.GetButtonDown("Fire1")&& Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;
            Shoot();

        }
    }
    IEnumerator Reload()
    {
        isReloading = true;
        reloading.SetActive(true);
        Debug.Log("Reloading");
        yield return new WaitForSeconds(reloadTime);
        currentAmmo = maxAmmo;
        reloading.SetActive(false);
        isReloading = false;
    }
   

    void Shoot()
    {
       

        currentAmmo--;
   

        GameObject bullet = Instantiate(bulletPrefab, firepoint.position, firepoint.rotation);
        Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
        rb.AddForce(firepoint.up * bulletForce, ForceMode2D.Impulse);
    }
}

You need to help us, help you mate. What’s not working? Is OnTriggerEnter2D being called (add a Debug.Log to it)? If not, does the bullet have a trigger collider?

Lines 29 and 30 of enemy script would be better placed in awake or start function, no? Also remove “gameObject” before finding reference to player. You are creating a new variable for player which I assume will be leaving your original reference null. The variable type is already assigned so no need to do again. Only

 player = gameObject.Find("player");

Something like that in start.

Remember the first rule of GameObject.Find():

Do not use GameObject.Find();

More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

It’s fine to use it in start or awake, with smaller projects. Yes there are better ways but also not a problem if used sparingly and correctly.

And yet, look how many new users post in here because GameObject.Find("Player"); fails to find their player, which happens to be named Player (Clone) .

The issue is that GameObject.Find() has 27 different ways to fail, most of them non-obvious, and a new user cannot reason about any of that. They wouldn’t even know where to begin!

I stand by the above. Sure, if you’re advanced and can reason about how it can fail, use it. It’s handy. But for anyone else, as the official docs recommend, do not use it.