How Scripts Communicate
Now that you know how two game objects can interact, let’s look at how you can access one script from another. To break it down, you can do this in four steps:
- Get a reference to the object
- Get and store the script component
- Verify the script component exists
- Access the method or variable
Get a reference to the GameObject
First things first, to access a script, you first need a reference to the object it’s attached to. There are multiple ways to get access to an object, such as:
- Create and serialize a class variable and assign the GameObject from the Inspector
- Create a class variable and assign the GameObject using the Find() method
- Access the GameObject from a collision method
Let’s focus on that last scenario. In surface collision methods, you have access to the Collision object, so you’ll need to access the other GameObject from that collection of information:
Similarly in trigger collision methods, you have access to the Collider component on the other GameObject, so you’ll need to access the GameObject the Collider is attached to:
No matter how you get a reference to the GameObject, you can start accessing other components on that GameObject once you have a reference.
Get and store the script component
By default, your active script only has direct access to a few components of the other GameObject, such as name and transform.
To access any other components attached to the other GameObject, you can use GetComponent(), which will look for the component type that you specify attached to the other GameObject.
When calling GetComponent, you specify the type of component within the angle brackets (< >).
To get a script component attached to a GameObject, you enter the script or class name as the component type.
Once we have the component we are looking for, we can store or cache the component in a variable for multiple uses, using the component type as the variable type.
Verify the script component exists
When using GetComponent, you tell the computer to get the component, but you do not receive a response indicating whether or not the component was retrieved.
If you try to access a variable or method on that component and the component was not found, Unity will throw the error above. To avoid throwing an error, you can check whether or not the component was successfully retrieved by null-checking.
Since you cached a reference to the script component, you can create an if-statement that checks if the script reference is not null. Then, enter your calls to script variables or methods inside of the if-statement. Unity will not throw an error because these calls do not occur if the Player script is not found.
Access the method or variable
If you recall from a previous article on variables, the access modifiers (public, private, etc.) before a variable or method determine whether or not it can be accessed by another script. So, once you have access to the script, you can access the public variables and methods from that script.
In the example above, Damage() is a public method on the Player class, which reduces the player’s life count by one each time it’s called. Now, each time the player collides with an enemy, the player will lose a life.
As you can see, script communication allows you to leverage in-game events, like trigger collisions, so that one object can alter another beyond the Physics engine. How will you use script communication to enhance your game?