How to reference a script in Unity
Introduction
As a game developer, you will inevitably find yourself in a position where you need to reference a script or component from another script.
Referencing scripts is essential for establishing communication and interaction between different components of your project.
In return, you can enhance the overall functionality of your projects.
In this article, I will provide you with a detailed guide on how to reference scripts in Unity. By the end of this article, you will know how to reference scripts in Unity.
Methods of Referencing Scripts in Unity
When we say "reference a script", what we mean is that you want to be able to access some data or method on one component in Unity from another component. Most often, this will happen when you want to access a component on a different game object. Sometimes, you will want to access a component on the same game object. We’ll walk through these two scenarios and explain best practices for how to reference another script in Unity.
There are two methods of referencing scripts in Unity.
- A. Manually setting up references in the Inspector.
- B. Using the Get Component method.
How to manually reference a script in Unity
You can set up a reference to a script manually using the Inspector.
You have to declare a reference variable of that script type. Create a reference variable to make a link to the script. If the script changes, the reference changes too.
If the reference variable is public or serialized, you’ll be able to see it on the Inspector. To assign it, drag an instance of the script to the empty field. To serialize a variable, you can use Unity’s Serialize Field attribute.
Example
You have a script called ScriptA already created in the Inspector. To declare a public reference variable to this script, it’ll look like this;
public ScriptA scriptA;
With this, you have successfully declared a reference variable to ScriptA. This gives you access to the script’s public data, variables, and functions using the dot operator.
scriptA.DoSomething();
where DoSomething is a function in ScriptA.
How to use the GetComponent method in Unity
In some cases, you may want to reference a script without setting it up manually in the Inspector. You can achieve this with the Get Component.
To use this method, ensure that both scripts are attached to the same GameObject.
Get Component is a method for finding a specific type of component or script in a GameObject.
How?
This method takes a generic type which is the type of script that the Get Component should be looking for.
Example
GetComponent<ScriptA>();
where ScriptA is the script you want to reference.
By default, Get Component only searches the component it is called on. You can use GetComponentInChildren or GetComponentInParent to reference not only the component but also the component’s parent.
To use this method, always check if the reference that the Get Component returns is not null before setting it up to avoid crashing the game.
Get Component is slower than the manual process of setting up references in the Inspector.
But, manually assigning scripts can be a hassle if you programmatically create or set up game objects.
How to pick the right method to reference a script
As a developer, you may find it difficult to choose which of the methods to use.
If your project is a simple one with a one-to-one relationship between objects, you should use the manual method of referencing.
On the other hand, if your project is dynamic and has multiple instances, you should use the Get Component method.
The bottom line here is that the referencing method you choose is based on your project’s structure, complexity and personal preferences.