The best practice when attaching scripts to game objects involving inheritance (and creating game objects in code)

Basically I have a game with characters using different inheritance subclasses. This game is more of strategy game so there isn’t one specific player character, but a virtual class Character with three subclasses (class in game) lets just say warrior, archer, and brute. Within each of those classes the character can be friendly or enemy. My question is how do I deal with inheritance in Unity since you attach scripts to game objects in order to use them? For the game objects in the scene, do I just apply the bottom most subclass to the game object, do I do the top most and do different code to link based on how the character is being used, or do I just attach all of the scripts to the object and deal with it?

Lastly, I plan on doing some procedural enemy generation, so I’ll eventually have to create a random amount of enemies that are randomly one of the three classes, so how does that work creating game objects within code. I can’t really grasp creating game objects without manually creating new game objects within the scene editor. I added this here because when I create the game objects I wouldn’t know what type I would create the object.

The best practices here match how you would instantiate inherited classes in your own code for a non-Unity project. Every script / behavior (Monobehaviour derived class) you assign to an object will be instantiated if that object exists in a scene.

You assign the most specifically derived class as a behavior to your game object, so that it has all of the options and features you need. If you were to assign multiple inheritance levels of the class, they would all be instantiated and run, which you wouldn’t likely want.

When making a public field to reference to that behavior in other scripts, you can use the type for the least level of inheritance that is needed. This way, you can handle polymorphic objects by their commonalities, if desired.

You should submit second question separately.