Alright guys!
Im trying to access a variable called “enemyHealth” thats inside a class called “Enemy”. The Enemy class will be applied to all Enemies through my game. The health etc ( such as mana, armor…) will be different on each enemy in the game(obviously).
Therefore i have created a gameObject called “currentTarget”. The currentTarget is defined when the player rightclicks on the target. (pseudo: currentTarget = hit.collider.gameObject) And when the “attack button” is pressed i want the enemyHealth to subtract equal to the amount of the playerDPS variable. I have alot of other references that works just fine, but i cant manage to refer to the variable created on runtime… ¿Comprende?
Here is some of the code:
void Start()
{
_GameMaster.GetComponent<Handler>();
_GameMaster.GetComponent<Player>();
}
void OnGUI()
{
//The code below should also be changed to " currentTarget.IsDead" etc.....
if(Handler.battleIsOn == true Player.playerIsDead == false Enemy.enemyIsDead == false)
{
GUI.BeginGroup (new Rect (Screen.width / 2 - 50, Screen.height / 2 - 50, 500, 500));
//Attack button
if (GUI.Button(new Rect(0, 300, 100, 100), btnAttackTexture, ""))
{
Debug.Log("Attack Button Pressed");
//currentTarget.Enemy.enemyHealth -= Player.playerDPS;
Player.UpdatePlayerStats();
Enemy.UpdateEnemyStats();
}
GUI.EndGroup();
}
}
void Update ()
{
//If player rightclicks on an enemy target, begin battle!
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit) Input.GetButtonDown("Fire2"))
{
if(hit.collider.tag == "Enemy")
{
Debug.Log("Battle Started!");
Handler.battleIsOn = true;
currentTarget = hit.collider.gameObject;
//currentTarget.Enemy.enemyHealth-=10;
Debug.Log("Current target is: " + currentTarget);
}
}
}
How can I refer to the currentTarget? The code below does not work…
If i understand this right i need to do something like “xxxx.GetComponent();”
I’ve tried to refer directly to the script by using Enemy.enemyHealth, that works fine but i want to be able to have a currentTarget for later usage!
What am i missing? Please be help me out with this!
currentTarget.Enemy.enemyHealth-=Player.playerDps;