Problem
-------
I have a GameObject with a number of scripts attached. I've designed the GameObject to be a 'base' object for moving entities in my game. The scripts handle things that will be common to all physical objects in the game: Being picked up/thrown, teleported, stowed in vehicles, moving in a direction, etc.
Some of the scripts store public GameObjects added via the inspector to instantiate when the object is spawned: The rendered GameObject, the RigidBody GameObject, and the triggers the object uses.
What is a good pattern to extend and reuse this 'BaseMovableObject'? For example, I'd like to extend the base into 'Items' and 'Characters'. Characters would further be extended into the different enemy types.
Proposed Solutions
------------------
I have tried/proposed the following ideas:
1. Save the object as a prefab called 'BaseMovablePrefab'. Drag the prefab into the scene, make the changes to give it the scripts a character requires. Save this new GameObject as 'BaseCharacterPrefab'. Repeat for additional child classes/child gameObjects. The problem with this approach is that it doesn't give me a persistent inheritance. Changes I make to 'BaseMovablePrefab' wont be reflected in 'BaseCharacterPrefab', and will have to be manually copied down the inheritance hierachy.
2. Create the base object programmatically. Instead of having a prefab with all the required component scripts attached, have a creation script that will attach all the composing scripts to the GameObject. While this requires a bit more scripting, and changes to this base 'SetupMovableObject' script will happen in any GameObject that has the script as a component.
3. Implement Subclasses as child objects. In this construction, a 'Character' would be a GameObject that would have a 'BaseMovablePrefab' as its parent transform. They would access scripts in the parent object to perform base functionality, and implement their own additional functionality (holding weapons, jumping, etc.) I think this will have the same problems as option 1, in that eventually I'll want to save a character as a prefab, which will create a new prefab version of the 'BaseMovablePrefab' GameObject anyway...
4. Use one prefab, but give it public variable enums that can be set (ie, AI vs Player controlled, Grenader vs Sniper vs Melee enemy type) or 'slots' that can be filled with GameObjects or Components (weapons scripts, movement scripts, etc).
5. Use a separate 'Builder' script that assembles GameObjects with the desired components. Give the builder script lists of the various prefabs (i.e, the triggers and rigid bodies common to all BaseMoveableObjects, and also the models and weapons for specific types of MoveableObject).
Discussion
----------
My inclination is to go with a combination of Option 2 and Option 5. So, I'm thinking I'll do as much as I can programmatically. Also, I'll seperate the scripts responsible for 'Building' and object from those responsible for 'Being' an object. If I can make the script create GameObjects I am using prefabs for at present (GameObjects for use as Triggers and Rigidbodies, primarily) that would be even better.
In past projects, I have gone with option 1, and spent a lot of time manually fiddling with 'Child' prefabs (i.e, prefabs that were created from an existing prefab). If I added a script to the 'base' object, or changed some parameter in it, I'd have to replicate that change manually to all the subtypes further down in the hierarchy.
What are other people's thoughts on creating extensible, modular, reusable GameObjects? Do you use prefabs (maybe with some Unity functionality I am unaware of), or mainly do things via scripting? How do you easily apply changes to a group of similar prefabs?
I like manipulating things via the editor and seeing those changes in a scene, but it doesn't seem to scale well. Are scripts that run in the editor itself (i.e, creating objects as a 'Builder' script is updated) a solution?
↧