How to call method from another script properly?

Helloooo everyone!

Alright, I’m trying to call a method from my EnemyController to my player Gun script so when an enemy is shot it reacts to it and basically chases the player. As expected, if I’m here it’s because I’m not doing this properly and can’t fix it on my own.

The issue is that I’m not 100% what the best way to call a method is. As what I did is made a variable public EnemyController enemyController; to reference the script. Problem is that since I did that, the script asks me to add one EnemyController in the inspector and when you have more than one enemy it doesn’t work at all.

Any help appreciated! Cheers :slight_smile:

Here are the small lines of code :

void Shoot()
    {
        currentAmmo--;
        RaycastHit hit;
        if (Physics.Raycast(transform.position + shootPointOffset, transform.forward, out hit, range))
        {       
            EnemyHealth target = hit.transform.GetComponent<EnemyHealth>();
            if(target != null)
            {
                target.TakeDamage(damage);
             
            
               // enemyController.enemyState = EnemyController.EnemyState.Chase; //these are the two lines that are causing me issues..
               // enemyController.agent.destination = enemyController.target.position; // second line
            }
           if (hit.rigidbody != null)
            {
                hit.rigidbody.AddForce(-hit.normal * impactForce);
            }

            GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
            Destroy(impactGO, 1f);
        }
        Debug.Log("effetcs");

        muzzleFlash.Play();
        cartridgeEffect.Play();

    }

Well, looking at what you have, you’re already doing a GetComponent call for EnemyHealth. Assuming that target has the EnemyController also, you can just get that component and assign it to your variable. Then call the method you want to on that variable.