How to retrieve a variable from another script

Basically title has it all, if possible in both c# and js but whatever help you can manage would be appreciated… Thanks

Hi @YourSeedyUncle,

Well there are couple of options depending on what you want to code and your coding style or requirements.

USING GetComponent

Scripts attached to the same game object

You can use the GetComponent function to access a function or a variable inside a script. Be aware to declare the variable or function you want to access from another script as public.
See the doc it contains a working example for both C# and javascript.

gameObject.GetComponent(ScriptName);

Scripts not attached to the same game object

If your scripts are not attached to the same game object you will first need to use the GameObject.Find function and then use the GetComponent function.

Another option is to create a gameobject variable in the script from where you want to access the other script variables, assign the target gamobject with that script and use that variable to get component.

MyTargetgameObject.GetComponent(ScriptName);.

USING STATIC qualifier

In C# sometimes it is easier to apply the static qualifier to your functions or variables and you will only need to reference the script to access them without the need to create gameobjects or use the GetComponent. It depends on what you want and need.

Example:

// A.cs script **********

public static bool myPublicStaticvar = true;


// B.cs script **********
// from where you want to access the variables

if (A.myPublicStaticvar)
... do stuff

i think there is a lot of documentation around for this. thats why daveA made that comment.

it depends on what u need to do. you can create static variables in a script. in the script declare a variable like ‘public static int num = 5;’. then to use it you can simply type the script name and the variable, like ‘int copyNum = someScript.num;’

otherwise to access public variables you need to create a reference to the script as it exists in the scene. for example say i need a transform variable from another script called ParkingLot. This script is attached to a gameobject called Parking Lot.

GameObject pl = GameObject.Find("Parking Lot");
ParkingLot parkingScript = pl.GetComponent<ParkingLot>();
//now get the variable from the script reference we just made
Transform parkingSpaceTran = parkingScript.parkingSpotTran1;

Instead of using GameObject.Find you could also just declare a public variable like ‘public GameObject parkingLot;’ and then drag the gameobject u want to reference from the scene into this variable via the inspector. then you can create a reference to whatever script you want thats attached to that gameobject. If it’s just the script you need you can also declare a public variable of a script. So I can say ‘public ParkingLot parkScript;’ and then drag the existing ParkingLot script in my scene into this public variable via the inspector. Finally, you can also use get/set methods if you want to keep the variables private.