Getting a variable from another script

Hello!
How do I get access to a variable from another script? I’ve seen other forums on this but none work for me.

Disclaimer: Make sure the variable in
your other script is public. This is
how you can tell:

Public:

 public float playerHealth;

Private:

private float playerHealth;

To get access to a variable from another script,
you first have to reference that script. This is how:

public ScriptName scriptName;

This goes in the same place as your
other variables.

Then, in your Start or Awake function, enter this code to correctly get the script so you can access the variable.

void Start() 
{
 scriptName = scriptName.GetComponent<ScriptName>();
}

then, you can do scriptName.variable to access the variable you want to access.

Example:

scriptName.playerHealth = 25f;

If you have any other questions, or if I am wrong, comment below.

Either make it public:

public int X;

or make it readable from outside,:

public int X{

public get;

private set;

}

Either way your class must be public.

This is basic of the basic stuff, it’s the 1st thing you learn after typing. I guess you are still learning to code but this is so basic that you shoudl’ve learned by yourself or searching on the web, not by asking on the Forum