Define a variable through project

Hello everyone!

I basically want to define a variable in a script that could be accessed from any scripts. More or less like the Screen.width or Mathf.Pi, etc.

I could use Players.Prefs but that’s not the way I want it, neither use GetComponent.

Any idea?

Thanks in advance!

The variable you want is static variable. To declare it, you have to use static keyword. In C#:

public static int myVariable;

from JavaScript:

public static var myVariable : int;

And then, assuming you declared it in MyClass script, you can access this variable using

MyClass.myVariable

from any other script.

You have to use Static or Const qualifiers on your variables. They will then be made accessible to all code in that project.

For example, a public class Variables contains a public static (or const) string LastName. You can then access it by Variables.Lastname

You can find a brilliant practical example here: Unity Connect

They use what I just outlined to access Tag strings across the project.

Just make it static and public.
public static VariableType VariableName.