How to pass variables to inherited classes in runtime

I have a base attack class that other attack classes inherit from. Some of the children are opponent based and need the “opponent” variable. However this variable is created in runtime when I mouseover an enemy, that enemy becomes the “opponent” variable in the base class. However it is not passed to the chidren even though it is public. Is there a way I could make sure that the opponent variable from the base class is passed to all the opponent based attack child classes when i mouse over an enemy?

My mouse over script:

public GameObject player;

void OnEnable()
{
	player = GameObject.FindGameObjectWithTag ("Player");
}

void OnMouseOver()
{
	player.GetComponent<Attack> ().opponent = gameObject;
}

Oh, I see what you’re trying to do now!
Yeah, I’ve never seen someone do that before, but that seems pretty useful.

The reason your player variable is “blank” (aka null) is because you created an instance of the base class and then initialized the player. This does not change the original base class that all the child classes inherit from, again because you just initialized a variable belonging to an instance of the class and not the class itself.

That said, I believe you have 2 options.
Option 1: Initialize the player variable for every instance.

public class Enemy : Human 
{
    public GameObject player;

    void Start() 
   `{
        player = GameObject.FindGameObjectWithTag( "Player" );
    }
}
        
public class TypeOfEnemy : Enemy 
{        
    void Update()
    {
        transform.LookAt( player.transform.position );
    }
}

Option 2: Make the player variable static ( which I think is not as flexible as option 1, but depending on your game this may be just fine ).

public class Enemy : Human 
{    
    public static GameObject player;
    
    void Awake() 
    {
        player = GameObject.FindGameObjectWithTag( "Player" );
    }
}
    
public class TypeOfEnemy : Enemy 
{    
    void Update()
    {
        transform.LookAt( player.transform.position );
    }
}