Subtracting health

Hey guys,

I’m doing a project where your job is to protect the CPU of a computer from the evil viruses. Almost everything is working and done, but I have one problem. I can not get the enemies to subtract from the health variables of either the character or the CPU. See the script below:

var Target : Transform;
var distance : int;
var Health : int = 3;
var tarmoney : int;
var moneySpawn : GameObject;
var player : GameObject;
var playerDistance : int;
var CPU : GameObject;
var isTargetPlayer : boolean;
function Start()
{
	player = GameObject.Find("Player");
	CPU = GameObject.Find("CPU");
	Target = CPU.transform;
	
	
}

function Update(){
	
	
	
	GetComponent(NavMeshAgent).destination = Target.position;
	playerDistance = Vector3.Distance(player.transform.position, transform.position); 
	distance = Vector3.Distance(Target.transform.position, transform.position); 
	if(distance < 3)
	{
		Attack();
	}
	
	if(Health > 3)
	{
		Health = 3;
	}
	if(Health < 0)
	{
		Health = 0;
	}
	if(Health == 0)
	{
	print("Dead");	
	Instantiate(moneySpawn, transform.position, transform.rotation);
	Destroy(gameObject);
	}
	if(isTargetPlayer)
	{
	tarmoney = Target.GetComponent(PlayerAttributes).Money;
	}
	if(playerDistance < 10)
		{
		Target = player.transform;
		isTargetPlayer = true;
		}
	else
	{
		Target = CPU.transform;
		isTargetPlayer = false;
	}
}
	

function Attack()
{
	SubtractHealth();
}

function SubtractHealth()
{
	yield WaitForSeconds(3);
	if(isTargetPlayer){
	var Targethealth = Target.GetComponent(PlayerAttributes).health;
	Targethealth --;
	}
	
	else
	{
	var CPUhealth = Target.GetComponent(CPUAttributes).health;
	CPUhealth --;
	}
}

function OnTriggerEnter(other: Collider)
{
	if(other.tag == "Shot")
	{
		Health --;
	}

}

Mainly this part:

function SubtractHealth()
{
	yield WaitForSeconds(3);
	if(isTargetPlayer){
	var Targethealth = Target.GetComponent(PlayerAttributes).health;
	Targethealth --;
	}
	
	else
	{
	var CPUhealth = Target.GetComponent(CPUAttributes).health;
	CPUhealth --;
	}
}

This gets called when the distance is < 3. Even when its 3 it doesnt ever subtract. Whats wrong here?

Thanks

Anyone? I thought this was a pretty straight forward question. Maybe I’m missing something else?

function removeHealth()
{
health = health - damage;
}
1 Like

What you should do is use the SendMessage() function, which I believe is faster then GameObject.GetComponent() anyways. For example, when the player attacks:

if(attack){
                target.SendMessage("ApplyDamage", damage);
           }

And then on the object the player is attacking:

function ApplyDamage(damage:int) {
                health -= damage;
                if(health <= 0){
                     dead = true;
                 }
           }

Something along those lines. gameObject.SendMessage() calls a function in a gameObject (in this case, “ApplyDamage”) and then fills in any parameters (in this case, the damage integer). So whenever ApplyDamage() is called, it will take away how much damage you put in the SendMessage(). For example, if you want to take away 10 health, the SendMessage would look like:

 target.SendMessage("ApplyDamage", 10);

…or if you want it to take away 69 health:

 target.SendMessage("ApplyDamage", 69);
1 Like

I tried what you said, and it works, but only half way. It subtracts so quickly that it goes into the negative 500s in less than 10 seconds.

Anyone?

I’m guessing here because you didn’t showed the “new” code.
Could it be that you Send the Message every Frame and dont have a cooldown or delay in place?

I tried doing that, but it didnt seem to work. Here is the script on the attacker:

var Target : Transform;
var distance : int;
var Health : int = 3;
var tarmoney : int;
var moneySpawn : GameObject;
var player : GameObject;
var playerDistance : int;
var CPU : GameObject;
var isTargetPlayer : boolean;
function Start()
{
	player = GameObject.Find("Player");
	CPU = GameObject.Find("CPU");
	Target = CPU.transform;
	
	
}

function Update(){
	
	
	
	GetComponent(NavMeshAgent).destination = Target.position;
	playerDistance = Vector3.Distance(player.transform.position, transform.position); 
	distance = Vector3.Distance(Target.transform.position, transform.position); 
	if(distance < 3)
	{
		Attack();
	}
	
	if(Health > 3)
	{
		Health = 3;
	}
	if(Health < 0)
	{
		Health = 0;
	}
	if(Health == 0)
	{
	print("Dead");	
	Instantiate(moneySpawn, transform.position, transform.rotation);
	Destroy(gameObject);
	}
	if(isTargetPlayer)
	{
	tarmoney = Target.GetComponent(PlayerAttributes).Money;
	}
	if(playerDistance < 10)
		{
		Target = player.transform;
		isTargetPlayer = true;
		}
	else
	{
		Target = CPU.transform;
		isTargetPlayer = false;
	}
}
	

function Attack()
{
	SubtractHealth();
}

function SubtractHealth()
{
	yield WaitForSeconds(3);
	 Target.SendMessage("ApplyDamage", 3);
}

function OnTriggerEnter(other: Collider)
{
	if(other.tag == "Shot")
	{
		Health --;
	}

}

and here is the script on the thing being attacked (CPU object)

#pragma strict
var health : int;
function Start () {
health = 100;
}

function Update () {

}
function ApplyDamage(damage:int) {
	yield WaitForSeconds(3);
     health -= damage;

     if(health <= 0){

      //

 }

}

Also derpy is best pony

I can’t find any errors, maybe yield WaitForSeconds(3); isnt working try to set up your own timer.
I can’t test it because it takes me 3 days to download Unity atm 1,6kb/s with 50mb connection. The servers are sloooooow…

Indeed it is :slight_smile: /)

#EDIT:
The documentation shows that you need to call SubtractHealth() this way: yield SubtractHealth();
And then the waiting should work :slight_smile:

Awesome Thanks. I will try and report back in a few minutes.

(\

EDIT: It seems as though its waiting at the very start. The enemies walk up to the CPU object, wait 3 seconds, and then go mad and subtract very quickly.

Here is the two scripts right now:

AI:

var Target : Transform;
var distance : int;
var Health : int = 3;
var tarmoney : int;
var moneySpawn : GameObject;
var player : GameObject;
var playerDistance : int;
var CPU : GameObject;
var isTargetPlayer : boolean;
function Start()
{
	player = GameObject.Find("Player");
	CPU = GameObject.Find("CPU");
	Target = CPU.transform;
	
	
}

function Update(){
	
	
	
	GetComponent(NavMeshAgent).destination = Target.position;
	playerDistance = Vector3.Distance(player.transform.position, transform.position); 
	distance = Vector3.Distance(Target.transform.position, transform.position); 
	if(distance < 3)
	{
		Attack();
	}
	
	if(Health > 3)
	{
		Health = 3;
	}
	if(Health < 0)
	{
		Health = 0;
	}
	if(Health == 0)
	{
	print("Dead");	
	Instantiate(moneySpawn, transform.position, transform.rotation);
	Destroy(gameObject);
	}
	if(isTargetPlayer)
	{
	tarmoney = Target.GetComponent(PlayerAttributes).Money;
	}
	if(playerDistance < 10)
		{
		Target = player.transform;
		isTargetPlayer = true;
		}
	else
	{
		Target = CPU.transform;
		isTargetPlayer = false;
	}
}
	

function Attack()
{
	yield SubtractHealth();
}

function SubtractHealth()
{
	yield WaitForSeconds(3);
	 Target.SendMessage("ApplyDamage", 3);
}

function OnTriggerEnter(other: Collider)
{
	if(other.tag == "Shot")
	{
		Health --;
	}

}

CPU:

#pragma strict
var health : int;
function Start () {
health = 100;
}

function Update () {

}
function ApplyDamage(damage:int) {
	yield WaitForSeconds(3);
     health -= damage;

     if(health <= 0){

      //

 }

}

You could try invoking it:

var attacking: boolean = false;

function Update () {

      if(distance < 3){
           if(!attacking){
                Invoke("SubtractHealth", 3);
                attacking = true;
           }
      }
}

function SubtractHealth () {

     target.SendMessage("ApplyDamage", 3);
     attacking = false;
}

This way the variable will keep switching on and off every three seconds while the distance is less than 3 and will attack at each interval.