How to access a variable through a gameobject created on runtime?

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;

Well if there is a script on the target object called “Enemy” (which from your example I’m assuming there is). It would look like this:

currentTarget.GetComponent<Enemy>().enemyHealth-=Player.playerDps;

I’d suggest doing it a better way by having some sort of ApplyDamage method on the enemy class rather than changing it’s value directly.

So it’d look like: currentTarget.GetComponent<Enemy>().ApplyDamage(-Player.playerDps); Then you can do other checks inside of it like determining if it needs to die or something like that.

Cheers!

Thanks man :smile:

Yeah, the plan is to implement a method that calculates and determines the damage based on alot of variables, such as Armor and damage types like magic, ranged, melee. Etc etc.

Im really glad you took your time to help me out with this, I really love when people gives me suggestions and are being specific!

Thanks agains mate!!