Enemy health is supposed to be 50hp/takes 5 hit to kill but after I kill one the rest are one hit.
I don’t have this problem before I added scriptable objects on my enemies, I use inheritance on my enemies and it works perfectly fine but a lot of tutorials say scriptable objects is useful so I tried to combine them but now the “hitPoints” are stacking into negative.
Im still a beginner so I don’t know what’s better and what’s the difference between inheritance or scriptable object. btw is use C#
This is the base class of the inheritance
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyClass : MonoBehaviour
{
[Header("Attachments")]
protected private Transform target;
public EnemyHealthBar healthBar;
public EnemyData Info; //The scriptable object
void Start()
{
target = GameObject.Find ("PlayerCharacter").GetComponent<Transform>();
Info.hitPoints = Info.maxHitPoints;
healthBar.SetMaxHealth(Info.maxHitPoints);
target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
}
public void Update()
{
Move();
}
protected virtual void Move()
{
if(target != null)
{
if(Vector3.Distance(transform.position, target.position) < Info.chaseRange)
{
if(Vector3.Distance(transform.position, target.position) > Info.stoppingDistance)
{
transform.position = Vector3.MoveTowards(transform.position, target.position, Info.movementSpeed * Time.deltaTime);
}
else if(Vector3.Distance(transform.position, target.position) < Info.stoppingDistance && Vector3.Distance(transform.position, target.position) > Info.retreatDistance)
{
transform.position = this.transform.position;
}
else if(Vector3.Distance(transform.position, target.position) < Info.retreatDistance)
{
transform.position = Vector3.MoveTowards(transform.position, target.position, -Info.movementSpeed * Time.deltaTime);
}
}
}
}
////////////////////////////////////////////////////////////////
public void ReceiveDamage(int damage)
{
Info.hitPoints -= damage;
healthBar.SetHealth(Info.hitPoints);
if(Info.hitPoints <= 0)
{
Dead();
}
}
protected virtual void Dead()
{
Destroy(gameObject);
}
////////////////////////////////////////////////////////////////
}
