Simple question about get Component

I am sure this is incredibly simple and I am making it way to hard - but here it goes anyway:

I am accessing another script using GetComponent. What I am trying to do is access a variable, isSelected, and find out if it is true. If it IS true then I want to use that in an if statement to turn on a GUI.

The end goal here is if my target is selected, then turn on their health bar. If they are not selected, then turn it off. Maybe this isnt even the right way of going about that?

Here is what I am trying but obviously its not working yet - this is the script that is on the enemy. I then have another script (PlayerAbilities) that is on the player and has the isSelected Variable.

function OnGUI()
{
		var other : boolean = gameObject.GetComponent(PlayerAbilities).isSelected;
		if(other == true)
		{
		GUI.Box(Rect(10,40, healthBarLength, 20), curHealth + "/" + maxHealth);
		}
}

Are you getting any errors? Did you make the isSelected variable in the class Public?

If the scripts are not on the same gameobject you will need to do something like this

function OnGUI(){
var other : boolean = GameObject.Find("Player").GetComponent(PlayerAbilities).isSelected;
}

Well, first thing - the code is fine. But …
You should not use “GetComponent” in Update or OnGUI functions. It works but you should not do it. Just remember it. It is for optimization reason. Call “GetComponent” in “Start” function and remember the component in variable. Then check the variable in Update and OnGUI functions.

Second, how do you use those scripts? You see… you call GetComponent from gameObject variable on the same gameObject. If this OnGUI code works on the player then the player’s “PlayerAbilities” component will be gotten. So if it is a situation when the player checks enemy’s “PlayerAbilities” then you need to get the enemy’s component.

You do it, for instance, with “GameObject.Fing(“enemy”)” function. As soon as you have the component, you will be able to check the enemy’s property, and not player’s.