- Home /
How to refer to a enemy health, from use that in some variables...
(in c++) for example...i want to player stop them auto-attack crpg style, when the opponent health goes to 0.
my conflict:
void EnemyisDead()
{
if (Enemy.health <= 0)
{
StopCoroutine(AutoAction());
}
}
and this is my auto attack system
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());
}
}
If I put Enemy.health im receive typical error set a instance of a object.
I want to change that to my opponent.health but this its wrong obiously...
This target system alocate in my enemy script :
void OnMouseOver() { Player.opponent = transform; }
thanks in advance to all
Enemy.health means, Go to the script called Enemy and get the variable health. In this case health should be a static public int(or float).
If you have a health script that is attached to the enemy. Change the health variable in that script to "static public" Then you will be able to use that variable in another script.
Answer by falac · Jul 28, 2015 at 04:30 AM
that's what I thought but not, if I'm doing something wrong because i tested in previous times and has not worked for me...
I get the null reference exception error in the line 24 for me that´s line is... "if (_enemy.health <= 0)"
for knowledge i use that player script.. and common script for all my players called "humans" using to enemy opponnent, and other commmon script for player opponent called "creature" and individuals scripts for an enemy calleds enemy, enemy02, enemy 03, etc.
i do that because i think i cant assign various opponets to my player... Example:
opponent.GetComponent(Creature).GetHit(damage);
and if i use the same script "Enemy" to all my enemies they cant coordinate in time.time attacks
please tell me what can i do to resolve that problem... or to do my enemy health non-static with this scripts
thanks to all
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;
private Enemy _enemy;
void Awake ()
{
player = this;
health = maxHealth;
_enemy = opponent.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;
}
}
}
using UnityEngine; using System.Collections;
public abstract class Creature : MonoBehaviour { public string name;
public int damage;
public int maxHealth;
public int health;
public float range;
public void GetHit(int playerDamage)
{
health = health - playerDamage;
}
protected abstract void Attack();
}
Answer by TonanBora · Jul 27, 2015 at 04:57 PM
The reason your getting that error, is because your trying to access the Enemy script itself, not an instance of the Enemy script. You need an Enemy variable to store the Enemy script that you wish to access. Do not make the Health a static, as Hexer suggested, as this will cause more problems than it fixes.
The problem with making the Health static, is that all enemies will share the same exact health. So, if you damage one enemy, all enemies will be damaged, and enemies will be spawned with the damaged Health instead of max health.
So, what you need to do, is create an Enemy variable, and then somewhere in your code, set that variable to the enemy your attacking.
For example:
private Enemy _enemy;
Then, where ever your setting the opponent variable, also do this:
_enemy = opponent.GetComponent<Enemy>();
Then finally, instead of calling Enemy.Health, call _enemy.Health.
Your answer
Follow this Question
Related Questions
Show only selected enemy's health bar 1 Answer
The name `target' does not exist in the current context 1 Answer
Can anyone help with me with my attack script and enemy script? 1 Answer
Melee range AI 1 Answer
Weapon and enemy health 0 Answers