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?