Hi everybody
I’ve written some new scripts and they work fine, but there’s one problem. Strangly I can only attack the enemy which is first in my Hierarchy panel. After he’s gone I can only attack the new first enemy in the Hierarchy panel and so on.
These are my scripts, Attackscript is attached to the player and the other 2 to the enemies.
Attackscript:
var distance = 10;
var myobject : GameObject;
var otherobject : GameObject;
var mytransform : Transform;
var othertransform : Transform;
var cooldown : boolean = false;
var activetexture : GUIStyle;
var inactivetexture : GUIStyle;
var usedtexture : GUIStyle;
var enemyhealth : EnemyHealth;
static var interval : float;
function Start (){
usedtexture = activetexture;
interval = 10;
}
function Update (){
myobject = GameObject.FindWithTag("Player");
otherobject = GameObject.FindWithTag("Enemy");
mytransform = myobject.GetComponent(Transform);
if(otherobject != null){
othertransform = otherobject.GetComponent(Transform);
enemyhealth = otherobject.GetComponent(EnemyHealth);
}
}
function OnGUI (){
if (GUI.Button (Rect(0,500,100,100),GUIContent("Attack"),usedtexture) && Vector3.Distance(mytransform.position, othertransform.position) < distance && enemyhealth.enemyhealth > 0 && cooldown == false){
enemyhealth.enemyhealth -= 50;
cooldown = true;
Timer();
}
}
function Timer (){
if(cooldown == true){
usedtexture = inactivetexture;
yield WaitForSeconds(interval);
cooldown = false;
usedtexture = activetexture;
}
}
EnemyProximity:
static var aggroDistance = 10;
var mytransform : Transform;
var otherobject : GameObject;
var othertransform : Transform;
var glitchCorrector : boolean = true;
var tickerInterval : boolean = false;
var tickerRate = 5.0;
private var nextTick = 0.0;
static var seppuku : boolean = false;
var enemyhealth : EnemyHealth;
var enemyproximityhealth : int;
function Awake (){
mytransform = gameObject.GetComponent(Transform);
otherobject = null;
othertransform = null;
enemyhealth = null;
}
function Update () {
if (ClassSelectionScript.classselected == true && otherobject == null && othertransform == null ){
otherobject = GameObject.FindWithTag("Player");
othertransform = otherobject.GetComponent(Transform);
enemyhealth = gameObject.EnemyHealth;
}
if (othertransform != null && Vector3.Distance(mytransform.position, othertransform.position) < aggroDistance && glitchCorrector == true){
ArcherHealthScript.curhealth += 10;
WarriorHealthScript.curhealth += 10;
MageHealthScript.curhealth += 10;
glitchCorrector = false;
}
if (othertransform != null && Vector3.Distance(mytransform.position, othertransform.position) > aggroDistance && glitchCorrector == false){
glitchCorrector = true;
}
if(othertransform != null && Vector3.Distance(mytransform.position, othertransform.position) < aggroDistance && tickerInterval == false && HealthScript.curhealth > 0){
ArcherHealthScript.curhealth -= 10;
MageHealthScript.curhealth -= 10;
WarriorHealthScript.curhealth -= 10;
tickerInterval = true;
}
if(tickerInterval == true && Time.time > nextTick){
nextTick = Time.time + tickerRate;
tickerInterval = false;
}
}
EnemyHealth:
var enemyhealth : int = 100;
var enemyproximityobject : GameObject;
var enemyproximity : EnemyProximity;
var drop : GameObject;
function Start (){
enemyproximityobject = gameObject;
enemyproximity = enemyproximityobject.EnemyProximity;
}
function Update (){
if(enemyhealth <= 0){
seppuku = true;
Destroy(gameObject);
var instance : GameObject = Instantiate(drop, transform.position+Vector3(0,0.2,0), transform.rotation);
ExperienceScript.curexp += 10;
}
}
I think there’s a problem with .FindWithTag.
How can I tell Unity that I want ALL the objects with the tag Enemy?
Please help me!