Reading a var from variable parents

Right now i have a bullet script which is ingerited from by 3 other kinds of bullets. In this prant bullet script, I have it read from a bool in the player’s script which direction the player is facing to base the bullet’s direction off of.

Now that i have to create an enemy script that is able to shoot the same bullets, i have come across my problem; the Bullet script uses

GameObject thePlayer = GameObject.Find("Player");  
PlayerController PlayerScript = thePlayer.GetComponent<PlayerController>();  
if(!PlayerScript.facingRight)  
{  
speed = speed*-1;  
}

So if an enemy wants to fire, i’d have to write an identical copy of this script and its children and substitute PlayerController with EnemyController if i want it to work the same way.

The Shoot() method in the PlayerController script instantiates a bullet at the player’s position, then checks the player’s direction specifically and moves that way.

in short, the bullet GameObject, which has its own script checks the player’s direction itself when executing Start().

How i understand this code is that as soon as the bullet is spawned, it looks around the world for the player’s facing direction, then moves in that direction.

what i want to change it into is that as soon as the bullet is spawned, it asks “who shot me?” and then looks for the facing direction of the prefabbed instance that shot the bullet.

the “Who shot me?” is the essence of my problem, really.

So what i’d like to know: Is there a way to accomplish this by saying “just get facingRight from whoever shot you, nevermind who that is” ?

Because that would really make my day.
i could provide you the script if needed.

Edit: fixed poor wording and added more info.

There are various solutions to this. You definitely don’t have to write an identical script (always a bad idea in programming), you can inherit both player and enemy from the same script, for example:

using UnityEngine;

public abstract class Controller : MonoBehaviour
{
    public bool facingRight;
}

public class PlayerController : Controller
{
}

public class EnemyController : Controller
{
}

public class BulletShooter : MonoBehaviour
{
    private void Update()
    {
        Controller controller = gameObject.GetComponent<Controller>();
        Debug.Log("Facing right = " + controller.facingRight);
    }
}

If your individual controllers have vastly different direction logic or already inherit different classes, you might use interfaces instead:

public interface IController
{
    bool IsFacingRight();
}

public class EnemyController : MonoBehaviour, IController
{
    private int direction;

    public bool IsFacingRight()
    {
        return direction == 0;
    }
}

public class PlayerController : MonoBehaviour, IController
{
    private bool facingLeft;

    public bool IsFacingRight()
    {
        return !facingLeft;
    }
}

public class BulletShooter : MonoBehaviour
{
    private void Update()
    {
        IController controller = gameObject.GetComponent<IController>();
        Debug.Log("Facing right = " + controller.IsFacingRight());
    }
}

There are other less elegant ways, but the above two are the typical methods to share functionality between classes.