- Home /
Answer by David_Rios · Jan 04, 2018 at 12:50 AM
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.
Or you could just simple do this
1st Create a GameObject public GameObject ObjectWithTheScriptYouWantToGet;
2nd
ObjectWithTheScriptYouWantToGet.GetComponent<NameOfTheScript>().NowThePublicVariablesHere
Answer by black_sheep · Jan 04, 2018 at 05:41 AM
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