Enemy wont deal damage
@Xavier78 I am trying to make my AI enemy deals damage to the player but i but there is a problem Assets\DealDamage.cs(52,24): error CS1061: 'HealthBar' does not contain a definition for 'Setup' and no accessible extension method 'Setup' accepting a first argument of type 'HealthBar' could be found (are you missing a using directive or an assembly reference?) can someone help me? this is the scrypt for the enemy. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class DealDamage : MonoBehaviour { public abstract class enemy : MonoBehaviour { [SerializeField] private Player _player;
int MoveSpeed = 4;
int MaxDist = 3;
int MinDist = 2;
// Used to setup class from scripting end, any Serialized fields should have an equivalent parameter here.
public virtual void Setup(Player player)
{
_player = player;
}
protected virtual void Update()
{
transform.LookAt(_player.transform.position);
if (Vector3.Distance(transform.position, _player.transform.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, _player.transform.position) <= MaxDist)
{
Attack();
}
}
}
protected virtual void Attack()
{
_player.Damage(10);
}
}
public class Player : MonoBehaviour
{
[SerializeField]
private HealthBar _healthBar;
[SerializeField]
private static HealthSystem _healthSystem;
public void Start()
{
_healthBar.Setup(_healthSystem);
}
// Used to setup class from scripting end, any Serialized fields should have an equivalent parameter here.
public void Setup(HealthBar healthBar, HealthSystem healthSystem)
{
_healthBar = healthBar;
_healthSystem = healthSystem;
}
// Wrapper method.
public void Damage(float dmg)
{
_healthSystem.Damage(dmg);
}
}
public class HealthSystem : MonoBehaviour
{
[SerializeField]
private float _health = 100;
[SerializeField]
private float _damageCoolDownTime = 0.1f;
private bool isBeingDamaged = false;
// Used to setup class from scripting end, any Serialized fields should have an equivalent parameter here.
public void Setup(float health, float damageCoolDownTime)
{
_health = health;
_damageCoolDownTime = damageCoolDownTime;
}
// Called to try and apply damage.
public bool Damage(float dmg)
{
if (isBeingDamaged)
return false;
_health -= dmg;
StartCoroutine(Damaged());
return true;
}
// Used to wait for damageCoolDownTime so enemy can't keep hitting us, and so only one enemy can hit us at once.
private IEnumerator Damaged()
{
isBeingDamaged = true;
yield return new WaitForSeconds(_damageCoolDownTime);
isBeingDamaged = false;
}
}
}
and this is the health bar scrypt using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class HealthSystem : MonoBehaviour { public int health; public int numOfHearts;
public Image[] hearts;
public Sprite fullHeart;
public Sprite emptyHeart;
void Update()
{
if (health > numOfHearts)
{
health = numOfHearts;
}
for (int i = 0; i < hearts.Length; i++)
{
if (i < health)
{
hearts[i].sprite = fullHeart;
}
else
{
hearts[i].sprite = emptyHeart;
}
if (i < numOfHearts)
{
hearts[i].enabled = true;
}
else
{
hearts[i].enabled = false;
}
//if (health > 0)
//{
// Destroy(gameObject);
// }
}
}
public void Damaged()
{
health -= 1;
if (health < numOfHearts)
{
numOfHearts = health;
}
}
}
i am new to unity and C# and coding so if someone help me i would be very thankfull. ( srry for my terrible English)
Your answer
Follow this Question
Related Questions
Fall Damage script not working 1 Answer
Pushing enemy makes them stop following the player 0 Answers
Add a public Tranform target from another scene 0 Answers
Urgent help with Health System in a 2D game 1 Answer
can't damage spawned prefab 1 Answer