Script Communication in Unity using GetComponent

This is one of the most important concepts to grasp and perhaps one of the more difficult. In Unity, we have something called script communication which we can be done by using something called GetComponent.

If we want to access a variable or method from Script “Player” and we need to call it from script “Laser” we need to create a reference to the respective script.

In order to use the script, we need to find within the object which is attached to and this is where we need GetComponent. GetComponent is a function that searches for the respective object that matches the type we specify.

The object “Spawn_manager” and the script attached in green “Spawnmanager”

And how do we access the script? By typing the reference variable followed by the name of the method that you need to call

The Method that we are trying to call has a specific function as we can see in the next example.