Best Practices for Creating Instances that Group Game Objects

I am building a game involving teams of players. The players are game objects. It seems natural to have a Team class that holds the members for that team as well as some blackboard information that the team as a whole knows.

I want to create these teams on the fly, but the teams have variable numbers of players (i.e., I don’t want to create a prefab for the whole team).

This means (I think) that it makes sense to have a constructor in my Team class. But the team’s individual members are prefabs, so I’d to create them using Instantiate().

Problem: To use Instantiate in the constructor of the Team class, the most natural option is for Team to derive from MonoBehavior… but if Team derives from MonoBehavior, you cannot construct a team using the “new” keyword. I don’t think AddComponent works either because I need to pass in information specific to the Team being constructed (not all Teams are the same).

What is the proper way to handle this.

What I did was create a script called Teams that does derive from MonoBehavior and then included a nested public class within Teams that does not derive from anything. So to create a new team I can do:

BlueTeam = new Teams.Team(teamName, members, positions);

where “members” is a list of prefab GameObjects and positions is a list of Vector3s.

But I’m very new to Unity, so I’m wondering if I’m going about this whole thing wrong.

AddComponent is fine. Just make a constructor-like function. So first add the component, AddComponent returns the component, save that in a variable. Create a function that you will use like a constructor to set the variables of the team and call that function right after AddComponent.