Polymorphism on member attribute

This issue has been difficult for me to search, largely because the keywords bring up similar, but not exactly the same scenarios.
.

Basically what I’m trying to do is have a single GameManager class with a list of Player objects, but each class that inherits from GameManager will have some child of Player as well. How can I construct an array in GameManager to reduce or eliminate the amount of casting necessary on the list in a GameManager child.
.

Here’s an example. The scene containing GameManagerChild is guaranteed to only contain PlayerChild objects.
.

public class GameManager : MonoBehavior
{
    protected Player[] _players;

    protected void Start()
    {
        InitializePlayers();
    }

    protected void InitializePlayers()
    {
        _players = FindObjectsOfType(typeof(Player)) as Player[];
    }
}

public class GameManagerChild : GameManager
{
    void Update()
    {
        DoSomethingWithPlayers();
    }

    void DoSomethingWithPlayers()
    {
        foreach (PlayerChild player in _players)
        {
            player.SomeMethodSpecificToPlayerChild();
        }
    }
}

.

One solution could be to make InitializePlayers abstract and each child of GameManager would need to populate the list with appropriate children of Player, but this would still require casting each time we want to act on an item in _player from a GameManager child.
.

Any better design suggestions?

I’m not sure what’s the point of all this. Maybe you should choose a better / more descriptive name of your class. Does your class really “mangage the game”? If not you should use a different name.

However from your description you may want to do something like this:

public class GameManager<TItemType> : MonoBehavior where TItemType : Player
{
    protected TItemType[] _players;
    protected void Start()
    {
        InitializePlayers();
    }
    protected void InitializePlayers()
    {
        _players = FindObjectsOfType<TItemType>();
    }
}

public class GameManagerChild : GameManager<PlayerChild>
{
    void Update()
    {
        DoSomethingWithPlayers();
    }
    void DoSomethingWithPlayers()
    {
        foreach (PlayerChild player in _players)
        {
            player.SomeMethodSpecificToPlayerChild();
        }
    }
}

Note that you can’t attach a “GameManager” to a gameobject as generic classes are not supported. However derived concrete classes do work just fine. So “GameManagerChild” can be attached to a gameobject.