Cleanest way to handle multiple classes accessing and modifying a group of components?

Hello! I am currently working on setting up the systems for a physics character that is built with 10+ connected joints, rigidbodies, and colliders.

Right now I’m directly modifying/accessing the values such as rigidbody.mass = 5f; This was working fine, but as the project is growing there are now multiple scripts that are accessing and changing a lot of values of each component based on different circumstances. Some scripts are just reading from them, but there are a lot of situations where I need to dynamically change values on the components. There is also the issue of multiple scripts trying to change the same value on a component. It’s turning into a nightmare and I feel like there has to be a better way to handle this.

It seems like I need some kind of middle layer to handle changing the component values in one spot, but I don’t see the best way to implement that in a way that would actually make things cleaner because the middle layer would have the same issues right?

Any suggestions would be very helpful. Thanks!

You may. The first thing you need to do is model what is changing and why.

For instance, is the mass changing because he’s growing? Perhaps there should be a growth manager that tracks growth and can be queried for his contribution to mass.

Or is another part of the mass changing because he’s picking up a heavy object? Perhaps there should also be a “carried object manager” that can be queried for that mass contribution.

Once you make ONE of these “mass providers,” make an ability to have a bunch of them that all contribute, then you can iterate whatever ones exist and add them up.

1 Like

Correct me if I am wrong. So your problem is that you have Joints that hold each a Collider/Rigidbody and you are accessing each and everyone depending on the situation, right?

If thats the issue, you could create a struct for each Joint that holds its own Collider/Rigidbody and maybe ID. All joints into a single Array and accessing with an identifier directly. Write your Methods for each struct.