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.
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 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();