- Home /
Scriptable object problems
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);
}
////////////////////////////////////////////////////////////////
}
Answer by Mrpxl · Jul 09, 2021 at 08:36 AM
You shouldn't use a ScriptableObject to store variables for your Enemy. What happens here is that all your Bomb Cube share the same object to check their health on so if you deplete some health on one enemy, all the others that check this same scriptableObject will see the depleted health. The scriptable object here is shared by all your enemies.
I would suggest you use Struct instead of that scriptable object that would contain all the enemy related variables. Or even just a float Health would do the job just fine
Your answer
Follow this Question
Related Questions
Item data structure - Scriptable Object Inheritance problems 1 Answer
How to Destroy the DontDestroyOnLoad() 2 Answers
How to activate a function on script B but its called from A 1 Answer
How do I make a photography function? 0 Answers
To pass a variable between scenes should I use scriptableobject or static ? 1 Answer