How to acess public variable of enemy from Player's script?

I have this problem: I want to know what is the health of enemy which I am attacking. I want to point mouse over enemies to show their health in status bar, it uses now Raycast to show that Enemy is under cursor, detects its transform, and thus I use SendMessage to adjust Enemy’s health when I attack or when I clone enemy. But SendMessage doesn’t return any result like functions do. But I know that in games it is usual to show enemy’s health and I also want that functionality. Does anyone know how to do it?
I declare some variables like EnemyHealth as public script, that is attached to enemy, but that seem to doesn’t allow to access it from external transform. At least Intellisense doesn’t show these user variables, and I am not sure if they will work.
My another thought was to create some class, so that its public variables are read by some functions like Set or Get etc, but again I habe no idea how to do that.
Any suggestions are welcome.

Check out this section of the manual, and if that doesn’t answer all your questions, feel free to post back!

yes, thank you. I solved this:
if (EnemyTransform != null)
{
EnemyScript = EnemyTransform.gameObject.GetComponent ();
EnemyHealth = EnemyScript.health;
}

1 Like