Altering a variable from multiple scripts

I am trying to be able to change a bool value from multiple scripts, but when I tried it made things stop working. A quick breakdown, I have a PlayerController script which controls the movement of the player, and also has a bool called “canMove”, and must be true for the player to be able to be moved.

In a TextBoxManager script I have called the PlayerController script with
“public PlayerController ness;”
then in Start() “ness = FindObjectOfType();”
and when a textbox is enabled
“canMove = false;”.

This worked fine, until I created a new script MenuController and tried calling the PlayerController script the exact same way, and would make “canMove = false” whenever the key to bring up the menu was pushed down.

Now my character is unresponsive to player input when the menu is called, but when talking to a NPC and the dialogue box appears, the player is freely able to move around (the TextBoxDialouge script is not setting “canMove = false” anymore). Is this not allowed in C# to call the FindObjectOfType in multiple scripts?? Or should I be referencing the script in another way? I eventually want the dialogue box and menu to be in the same graphic, but baby steps, I’m still very new to game design and programming.

Well, the problem probably isn’t where you disable movement but when you enable it from 3 different scripts. Say when you have the menu open AND talking to NPC, what happens when you exit the menu? I’m guessing the menu script will try to enable movement even when the NPC condition is still not met.

So to solve this problem, I think you should manage this movement thing in a completely new script or in the player movement script.

This script will check for ALL the conditions for enabling movement and return a boolean or something. The player script will then check the returned boolean to see if the player can move or not.

For example:

bool CanPlayerMove() {
    if (MenuOpen || NPCTalk || Other Conditions)
        return false;
    else 
        return true;
}