How to access VARIABLE from another Script in Unity

Introduction – We have a little problem…

In this article I am going to show the detailed process to access a variable defined in another script in Unity.

The main problem we are facing is the following, yesterday a person came in to ask how to read the defense value of an enemy from the player’s script, a while ago, just before you, someone came to ask how to make a control script access the total amount of coins collected to know if the objective has been achieved. And now you are here with your own project different from the others, with your own scripts to which you have given very particular names.

To solve this problem I will explain the solution USING GENERIC NAMES (such as “ScriptA” and “ScriptB“), you need to understand the procedure for this particular example and then you need to apply what you have understood to your own particular case, i.e. you need to determine who ScriptA and ScriptB are in your particular case.



All the IMPORTANT information is summarized in the following TUTORIAL FROM MY YOUTUBE CHANNEL


Step-by-step procedure to access a variable from another script in Unity

Step 1. Define which scripts are involved

This is a very important step for us to solve the naming problem, your scripts have a very particular name, different from the scripts of the other people who have entered in this article.

Please keep these two points in mind:

  • In this generic example we are going to use two scripts, their names will be “ScriptA” and “ScriptB“.
  • The variable we want to access is located in ScriptB and we are going to access this variable from ScriptA.



Step 2. Ensure that the variable is accessible from an external context

We must make sure that the variable we want to access (in ScriptB) is declared with public visibility, so that this variable can be accessed from an external context, for example from ScriptA.

For example, if the variable we want to access were of type int and its name were “exampleVariable“, in ScriptB we should define it as follows:

public int exampleVariable;

IF THE VARIABLE DOES NOT HAVE PUBLIC VISIBILITY IT CANNOT BE ACCESSED FROM OTHER SCRIPTS

Step 3. Define a data type that allows access to the script where the variable is located

Let’s remember that from ScriptA we want to access ScriptB, therefore inside ScriptA we have to define a ScriptB type variable, this variable will allow us to access from ScriptA to the variables and public functions defined in ScriptB.

We can define this variable as follows:

public ScriptB typeBVariable;



Step 4 (CRUCIAL). Initialize the above variable to point to the component we want to access

I can’t tell you how IMPORTANT this part is, not only to solve this particular problem of accessing a variable from another script, but for ANYTHING WE WANT TO DO IN UNITY. It is about being able to reference a precise object from a Script.

The point is that the variable that we defined in the previous step, if we do not initialize it, has a null value and if we try to use it in this state we will get a Null Reference Exception error in the console.

We have to make sure that the variable has in its interior the appropriate component to which we want to make reference, in this particular case the ScriptB type component.

This can be done in many ways, one of the simplest is to go to the Unity scene and in the ScriptA inspector drag the GameObject that has the ScriptB assigned to the ScriptB type field, in this way we place in the variable that component and we can use it inside the ScriptA.

Step 5. Use the ScriptB type variable to access the variable defined within that script

Once we completed the previous step we now have a ScriptB type variable that contains the reference of the ScriptB type component that is assigned in a GameObject in the Unity scene.

To access a variable defined in the other script we use that ScriptB type variable and with the dot operator we access the variable we are interested in. According to the example given in this step by step we would do it in the following way:

typeBVariable.exampleVariable

The previous expression is the variable that is defined in the other script, this variable can be used inside ScriptA or change its value if we wish.

Scroll to Top
Secured By miniOrange