Inter-script communication and Dot Syntax
In order to create games effectively, you'll often need to communicate between scripts in order to pass data around, adjust variables, and call functions in external scripts—by external here we can mean either a separate script or one attached to a different object than the given script.
Accessing other objects
Often you may be in a situation where your script is located on one object, and you wish to communicate with a script on another object—for example, your player character may shoot an enemy and this results in the need for their health to decrease, but each enemy has an independent script storing its own health, so a script on the player or bullet must address the script on the enemy that its health is stored within.
To do this, prior to accessing the script, you'll need to refer to the object, which can be done in various ways including using the Find()
and FindWithTag()
commands or in the case of a collision, by referring to the collided with...