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:

  1. Get a reference to the object
  2. Get and store the script component
  3. Verify the script component exists
  4. Access the method or variable

Get a reference to the GameObject

  • 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:

Getting a reference to the other GameObject in a surface collision method

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:

Getting a reference to the other GameObject in a trigger collision method

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

Tool-tip showing default methods and components on the other GameObject

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.

Using GetComponent to access the MeshRenderer on the other GameObject

When calling GetComponent, you specify the type of component within the angle brackets (< >).

Using GetComponent to access the Player script on the other GameObject

To get a script component attached to a GameObject, you enter the script or class name as the component type.

Cache references to components in variables for future use

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

Unity’s Null Reference Exception

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.

Example of null-checking the Player script

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

Within the null-check, call public methods and variables from the 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?