Thoughts on using avoiding monobehavior as much as possible? Example provided in the thread

The amount of components in my inspector window tends to rack up quickly for my character classes with the player having a component for each behavior.
I want to see if I can reduce the clutter by using monobehaviors only for handling the visual elements or anything related to transforms in the world. Basically, looking at monobehaviours as a way to represent a game object thats rendering.

For example, I want to create a pistol in my scene for the player to shoot.

  • The player weapon handling class will hold this data rather than the object in the world.
  • The pistol class (not instantiated in the scene) holds the logic of what should happen if the player shoots.
  • It holds base data such as reload speed, fire rate, damage, etc.
  • Things that are relative to whats happening in it’s own instance is handled by a monobehavior class in the scene. It contains coroutines for reloading and shooting but the coroutines arent stored inside of it and neither is localized data such as time before next shot and current ammo.
  • This is because there may be other players or classes that use this pistol class too.
  • As for the physical representation, it would either contain an object id or prefab inside the class.

I mean this is why we have scriptable objects: so we can store behaviour and data into assets that don’t live on game objects.

A lot of the data for say, a weapon, is immutable. Thus it makes sense to push that onto a scriptable object that can be reused across multiple monobehaviours.

Per-instance data (ammo count and such) would need to in or on monobehaviours. Though if some of this data needs to be written to disk for save games, it ought to be encapsulated in plain C# classes then.

More monobehaviours isn’t necessarily a bad thing, so long as they’re broken up by clear responsibility. The player character in my current project has about 15+ custom monobehaviours on it. But it’s organised as opposed to cluttered.

2 Likes