Script Communication in Unity using GetComponent
Every GameObject in Unity has a set of components to define its behaviour and properties. For example a 2D object has a transform and sprite renderer. We could access these components in a script using the method GetComponent and execute functions and modify their values.
In this example I’m gonna change the color of the layer to red when colliding with an enemy. In the enemy script within the OnTriggerEnter2D() Function we have to detect the player and get the player sprite renderer reference as below.
This is how you can use GetComponent(), but they have more uses. You could damage the player, change its size and do any operation. You just have to get the component that you want to utilize and play with its properties.