Acces value of Variable from other script

Hello

I try to describe my problem real quick, so
Right now i have two scripts in first one is

Script name : playerControls1.js
script is attached to “Player_Resizeable” gameObject

blah blah
public var name_of_variable	: boolean = false;
blah blah

in second one

function Awake (){
	var test : GameObject;
	test     = GameObject.Find("Player_Resizeable");
	
		if ( test == null )
 		   Debug.Log("Something is messed up");
		else
		    Debug.Log("everything is peachy");

	var playerControls1 : playerControls1;
	playerControls1 = test.GetComponent( "playerControls1" );
}

When i want to acces variable from playerControls1.js i write

if ( playerControls1.name_of_variable )          // for example...

aaand it give me this error

it also give me back “everything is peachy” from debug log… and I was searching hours for answer but I cant find something that helps :-x
Please anyone ?

im not super familiar with UnityScript, but im pretty sure the variable name can not be the same as the variable type… maybe something more like var playerControls : playerControls1; also you should try test.GetComponent(); instead, and all of your Scripts should really start with a capital letter…

or you can mark youre variable as static…
see link global variables.

Yes, you can do that, but that leads to bad program design, really he should be using get and set methods, its a concept of polymorphism and variable hiding, you should have few public variables really… the only ones that should be public are the ones you want to modify in the inspector, everything else should be private, and if it needs to be accessed by another script, you should have getter and setter methods set up…

I don’t even use public for the inspector, i instead use [SerializeField] attribute.

That work in C#? I think ive used [System.Serializable] before or something like that for getting my self made classes to display…

Never mind, i answered my own question…

yep i hear that i shouldn’t use static-global var’s, so I’m not going to do it this way, or if you cant help me with this can you suggest any other solution to access value of the variable from another script, something that works pls

edit: thanks mike but that dont work either it seems that i wrong when i call if ( playerControls1.name_of_variable )
because this is how you call static var’s but i cant find any other calling method from variable

Best to get proper naming convention going on. All classes (scripts) should start with a capital letter. So like

PlayerControls1.js not playerControls1.js.

Then to access other variables is a matter of getting an instance of the script. So once you renamed your script to the appropriate capitalization, then you can do:

var playerControls1 : PlayerControls1 = test.GetComponent(PlayerControls1);

then you can easily access any public accesible variables/methods by playerControls1.whatevervariable

EliteMossy thank you, that works :slight_smile: some other things changed but it works, for now :smile: