How to watch variables from another script efficiently?

Good evening!

unity 5.3.4, c#


How would I continuously watch a variable from another script in a very efficient manner?


Scenario/Example:

  1. I have a HighLevelControllerScript class with a variable field public GameObject checkThisObject;

  2. I drag and drop my PlayerController GameObject into the checkThisObject field in the inspector.

  3. The PlayerController has a script attached to it called PlayerManager.

  4. Now, in that HighLevelControllerScript class’s Update() I do a Int lvlCheck = checkThisObject.getComponent<PlayerManager>().level;

  5. Then run an if statement on that int to check if level == 20. If so… do something amazing… like get a daedric katana.


Psudo Code:

public class HighLevelControllerScript : MonoBehaviour {

        //Set vars
        public GameObject checkThisObject;

        void Update(){
                Int lvlCheck =  checkThisObject.GetComponent<PlayerManager>().level;
                if(lvlCheck == 20){
                        Debug.Log("You just received a new weapon... check your inventory"); 
                }
        }

}//End Class

Is that the best way to go about watching a variable from another script? Seems to me that would be really inefficient.

And coroutines, don’t seem to be a good option.

Any help, provoking thoughts or points in the right direction would be greatly appreciated!
Thank you!

Observer pattern - Wikipedia May be a step in the right direction. But is it really needed to do the check every frame? You could also implement some kind of notification system which notifies the HighLevelControllerScript when the player has leveled up.