Hi everybody
I have a really big problem with my game. I want to make a script, attached to my GUI object containing all the other GUI, which will deal damage to the enemy when clicked on the button it creates, but after clicking it, you need to wait 3 seconds before you can deal damage again. So I tried doing it with this script, made in the same way as I saw in a tutorial.
var distance = 5;
var mytransform : Transform;
var othertransform : Transform;
var canAttack : boolean = true;
function OnGUI (){
if (GUI.Button (Rect(0,500,100,100),"ATTACK!") &&
Vector3.Distance(mytransform.position, othertransform.position) < distance &&
EnemyProximity.enemyHealth > 0 && canAttack == true){
Attack();
}
}
function Attack(){
canAttack = false;
EnemyProximity.enemyhealth -= 50;
yield WaitForSeconds(5);
canAttack = true;
}
In this script, distance is the distance needed to attack the enemy, mytransform is the transform of the player and othertransform the transform of the enemy. In the inspector I assigned the transforms. When I click the button, no errors appear, but the enemy, in whose script I told it to destroy itself when its health reaches 0, stays. This is the script of the enemy :
static var aggroDistance = 10;
var mytransform : Transform;
var othertransform : Transform;
var glitchCorrector : boolean = true;
var tickerInterval : boolean = false;
var tickerRate = 5.0;
private var nextTick = 0.0;
static var enemyHealth : int = 100;
function Update () {
if (Vector3.Distance(mytransform.position, othertransform.position)
< aggroDistance && glitchCorrector == true){
HealthScript.curhealth += 10;
glitchCorrector = false;
}
if (Vector3.Distance(mytransform.position, othertransform.position) >
aggroDistance && glitchCorrector == false){
glitchCorrector = true;
}
if(Vector3.Distance(mytransform.position, othertransform.position) <
aggroDistance && tickerInterval == false && HealthScript.curhealth > 0){
HealthScript.curhealth -= 10;
tickerInterval = true;
}
if(tickerInterval == true && Time.time > nextTick){
nextTick = Time.time + tickerRate;
tickerInterval = false;
}
if(enemyHealth <= 0){
Destroy(gameObject);
}
}
Please help me to solve this problem.