Best practice for 'cleanly' sharing properties among multiple behaviour scripts?
Hello,
My Player game object has multiple player scripts attached, a MovementController, CollisionController etc.
I am at a point where various mechanics need access to some core player properties such as playerHealth
, (or 'isDoingSomething' type properties).
What is the best practice for exposing such properties to all the scripts?
Currently I have set up a central PlayerScript which stores all the key properties which is then accessed by other behaviours through getComponent<PlayerScript>().playerHealth;
Although somehow this doesn't quite sit right with me. It also gets confusing as to what properties should be where, ie. IsJumping
.
One key issue is that certain properties like isJumping are core to the movement controller and reside in that class. But it is not uncommon that another script would needs to know the value of isJumping also.
Cross linking between component seems pretty common in the Unity examples, however as a project grows it becomes a hot mess quite quickly. Obviously using a single class for all player behaviours would eliminate such problems. But that's not really the solution.
I'd love any insight by anyone who has run into similar challenges in larger projects. Thank you!
Your answer
![](https://koobas.hobune.stream/wayback/20220612123404im_/https://answers.unity.com/themes/thub/images/avi.jpg)