Script Communication
When the life of the player is '0', we know that the boolean "isGameOver" is set to "true" in PlayerController.cs script. Now, the first thing I would like to do when isGameOver == true is to stop the movement of the objects. For this we need to let the MoveObjects.cs script know what is happening in our PlayerController.cs script. This is achieved with script communication.
Script Communication
Script Communication Workflow
Declare a variable that stores PlayerController script: The first step in script communication is to declare a variable of the type Class ("Class Name").
Search for an object that contains PlayerController script:
One way of doing this is using GameObject.Find() method and searching for the name of the game object that has PlayerController script in it
playerControllerScript = GameObject.Find("Player") ....
Another way is to create a GameObject variable in the MoveObjects script and cache the reference of the object that holds the playerController script in inspector window.
public GameObject gameObjectWithScript;
GetComponent<>():
The final step is to get the component and store is to our variable playerControllerScript declared in the Start() method.
playerControllerScript = GameObject.Find("Player").GetComponent<PlayerController>();
OR
playerControllerScript = gameObjectWithScript.GetComponent<PlayerController>();
MoveObject.cs Script
Now that we have a variable that we can use to communicate with our PlayerController script, I can move the objects only if the boolean isGameOver is set to true in PlayerController.cs.
A practical implication of access modifier can be seen in this process. In next section, I will talk about what access modifier is and how it affects the accessibility level in the script.