- Home /
how to refer to enemy health
Please here i have a serious dude... in vari enemy.health... enemy isnt a agameobject and i cant asign it i want to asign here my opponent (target) and stop my player auto attack when enemy health goes to 0. how can i do that???
void EnemyisDead() { if (enemy.health <= 0) { StopCoroutine(AutoAction()); } }
and here this the completly script
using UnityEngine; using System.Collections;
public class Player : Humans { public static Transform opponent;
public static bool isAttacking;
public static Player player;
Animation animation;
public AnimationClip attack;
public float attackImpact;
Enemy enemy;
void Awake ()
{
player = this;
health = maxHealth;
enemy = gameObject.GetComponent<Enemy>();
animation = GetComponent<Animation>();
initAnimations();
isAttacking = false;
}
IEnumerator AutoAction()
{
yield return new WaitForSeconds(1);
if(opponent != null && Vector3.Distance(opponent.position, transform.position) < range)
{
isAttacking = true;
animation.CrossFade(attack.name);
StartCoroutine(AutoAction());
}
}
void EnemyisDead()
{
if (enemy.health <= 0)
{
StopCoroutine(AutoAction());
}
}
//Initialize animations
void initAnimations()
{
animation = GetComponent<Animation>();
AnimationEvent attackEvent = new AnimationEvent();
attackEvent.time = attackImpact;
attackEvent.functionName = "impact";
attack.AddEvent(attackEvent);
}
void impact()
{
opponent.GetComponent<Creature>().GetHit(damage);
}
void Update ()
{
Attack();
EnemyisDead();
}
protected override void Attack ()
{
if(Input.GetKeyDown(KeyCode.Alpha1))
{
if(opponent != null && Vector3.Distance(opponent.position, transform.position) < range)
{
isAttacking = true;
animation.CrossFade(attack.name);
StartCoroutine(AutoAction());
}
}
if(!animation.IsPlaying(attack.name))
{
isAttacking = false;
}
}
}
and here is the another
using UnityEngine; using System.Collections;
public abstract class Humans : MonoBehaviour { public string name;
public int damage;
public int maxHealth;
public int health;
public float range;
public float attackRate = 0.0f;
public void GetHit(int playerDamage)
{
health = health - playerDamage;
}
protected abstract void Attack();
}
thanks in advance
Is this line working?
void EnemyisDead() { if (enemy.health
Or your problem is accessing to enemy.health?
Answer by Tonks · Jul 28, 2015 at 04:34 PM
StopCoroutine can cause a Null Ref, try assigning the coroutine to an IEnumerator Variable first.
For example, at the start, declare an IEnumerator called AutoAction, and rename your coroutine to AutoActionCoroutine.
When you call the coroutine, define AutoAction = AutoActionCoroutine, then call StarCoroutine(AutoAction); (note the lack of brackets).
Then, when you stop, call StopCoroutine(AutoAction);. It's much more memory efficient, and can prevent Null Reference Exceptions that often occur when you start the coroutine again.
ok so thanks to all thats work now but only with 1 enemy attach at it
i have serious problem now to do that with various enemies, Nido can you see me how can i use that for various scripts in one list example... enemy001 enemy002 enemy003 enemy 004... with this...
private Enemy[] enemy;
private void Awake() { enemy = FindObjectsOfType(typeof(Enemy)) as Enemy[]; }
the correct form of coroutine is like that so thanks that opens my $$anonymous$$d
for stop StopCoroutine("AutoAction");
for start StartCoroutine("AutoAction", 1.0f);
coroutine IEnumerator AutoAction() { yield return new WaitForSeconds(1); if(opponent != null && Vector3.Distance(opponent.position, transform.position) < range) { isAttacking = true; animation.CrossFade(attack.name); StartCoroutine("AutoAction", 1.0f); } }
With multiple enemies, having that Enemy[], inside the coroutine:
foreach (Enemy oneEnemy in enemy)
{
//Do all stuff.
}
This will work for each enemy but one after one. For work with all them in parallel, you may launch a new coroutine for each enemy, and giving the enemy as argument for the coroutine.
foreach (Enemy oneEnemy in enemies)
{
StartCoroutine(DoStuff(oneEnemy));
}
IEnumerator DoStuff(Enemy enemy)
{
enemy.transform.........
........................
}
Answer by Nido · Jul 28, 2015 at 12:43 PM
Try in enemy class:
private int health; //assign value
public int Health
{
get {return health;}
set {health = value;}
}
Then:
void EnemyisDead()
{
if (enemy.Health <= 0) //"Health" instead of "health"
{
StopCoroutine(AutoAction());
}
}
If "enemy" returns null, make it public and attach it:
public Enemy enemy;
And delete the line where you try to GetComponent();
If it's not possible to attach all enemies or any other problem, make it dynamic:
private Enemy enemy;
private void Awake()
{
enemy = FindObjectOfType(typeof(Enemy)) as Enemy;
}
//...or with many enemies...
private Enemy[] enemy;
private void Awake()
{
enemy = FindObjectsOfType(typeof(Enemy)) as Enemy[];
}
Your answer
Follow this Question
Related Questions
enemy AI follows exact path 2 Answers
Enemy AI Range Detection 3 Answers
Enemy AI 2D not working? ( Rigidbody.Addforce() ) 2 Answers
enemy raycast to detect player 1 Answer
Enemies should check if they are line of side to the player before attacking. 0 Answers