Ground Control to GetComponent: Script Communication in Unity using GetComponent
When programming in Unity it becomes increasing important to have inter-script communication so that different aspects of your project can inform and interact with one another. A simple way to do that is by accessing GetComponent() of a GameObject to access different aspects of it.
For example when a player attacks an enemy with a projectile and the enemy must determine what to do when coming in contact with the projectile, you can figure this out by using GetComponent<Enemy>() to access it’s health.
In this example we’ll look at when a simple sphere rolls into a enemy cylinder, destroying it and updating a score counter with some points.
Let’s start by opening (or creating a new Unity project) and then populating it with a plane (so the ball can roll around) and a Sphere with a Rigidbody component. Then we’ll attach the Player script I’ve provided below to the player allowing you to move around using WASD or the Arrow keys.
Next we’ll create a ScoreManager to keep track of the player’s score and update a UI text object. So let’s go ahead and create an empty GameObject in the Hierarchy named “Score Manager” and then create a C# script with the same name attaching it to the Score Manager.
Then create a Unity Text UI object by Right Click > UI > Text and set it to the middle of the screen using the anchoring tool, I typically type in what I want it to say as a reminder but we’ll set that in the code. I renamed it to “Score Text” here as well.
Here is the code for the Score Manager commented for your convenience, essentially it holds the score variable and the text object and when enemy dies it updates the score text. It is always good to separate out the functionality of your scripts so that they are easier to understand. This illustrates why script communication is so fundamental as it allows objects to talk to one another while keeping them neat and readable.
Don’t forget to set the Score Text to the Score Manager
Next we’ll set up an enemy cylinder. Attach a Rigidbody component so it can detect collisions with the Player and then create a new script named “Enemy” and attach it to the cylinder.
Now we’ll set up the enemy script, I’ve included in below with heavy comments but essentially we set a pointsValue for the enemy for when the player hits it. Then create a private ScoreManager variable that we assign programmatically.
Then using OnCollisionEnter (which we learned here) when the player is detected as the other object (using GetComponent) we update the ScoreManager by calling UpdateScore() and then destroy the Enemy GameObject ending the script.
Here’s the end result in action!
Now let’s illustrate what’s happening in case it isn’t clear. Spending about a million hours in MS Paint we have a example of what’s happening.
In a jist, when the Player rolls into the Enemy, the OnCollisionEnter() method in Enemy detects that the player has hit it by using GetComponent() to look for the Player script component on the Player.
It then updates the score in the ScoreManager variable that it set in Start() method by looking for any Object in the scene with ScoreManager component.
Then Enemy calls the UpdateScore method in ScoreManager to increment the score and update the text. Finally the enemy object destroys itself after updating the score.
This chain of events is only possible with script communication that using GetComponent() and FindObjectOfType() offers, there are other more performant and efficient ways to do this but this is a perfect way to show the power that script communication has. You can use this update ammo in a gun, how much health a player has, money in the bank, and pretty much any sort of complex behavior in Unity. Until next time, happy coding.