- Home /
Multiple MonoBehaviours of the same type on single object ?
Is it good practice to put multiple components of the same type on single game object ? To clearify lets get some example:
I have a configurable (by player) spaceship. Player can add many weapons to his spaceship, for example he can add 2 (or more) guns, so when he fires once, spaceship spawns multiple bullets (one for each weapon).
I wonder if its ok to code my weapon as MonoBehaviour script, and then, during loading spaceship just add multiple Gun scripts to spaceship object.
Alternative is to code only one MonoBehaviour called "WeaponComponent", then code weapons as simple classes, and keep all instances of Weapon class in an collection located in WeaponComponent.
I know both ways works, but i just wondering which one is considered as "best practice".
A third option would be to let each gun have their own GameObject with their own $$anonymous$$onoBehaviour script, and child the GameObject under the ship. That way you can easily have the guns transform easy accessible too :)
Answer by whydoidoit · May 05, 2013 at 02:52 PM
I've used both techniques - the benefit of using MonoBehaviours is nice simple encapsulation, the disbenefit is finding the right component when you do a call. Sometimes it can be nice to have the WeaponComponent approach and make your weapons ScriptableObject derivatives as this gives you a way of adding them as Resources or Asset Bundles.