Script Communication in Unity:
Having communication between scripts is essential if you want to access functions or components between game objects. One must first obtain the root access of the component with the GetComponent<> method.
Using the hierarchy, one can go down the branches to obtain the component needed. For example, let’s imagine two objects collided, and you have an on-trigger function. You can now access the script of the collided object by going down its hierarchy.
Get Abner Carmona’s stories in your inbox
Join Medium for free to get updates from this writer.
Something like: “other.GetComponent<Player>().Damage()” will go to the Player component (this case it’s a script) and initialize the Damage function.
But what if the component does not exist? It can cause a bug or an error in the system, so it’s important to check if the component exists, or in other words, a null check. You can null check with an if statement. This creates a more optimized system capable of error handling.









