- Home /
My Health Bar Script Won't Work When I Turn My Enemy Into A Prefab
I'm new to Unity and the solution to this problem is probably really simple but I haven't found the answer anywhere else so I figured I would ask here. I followed this tutorial on how to make a health bar ( https://www.youtube.com/watch?v=BLfNP4Sc_iA ) and it worked great. In the tutorial, the "deal damage" function was on the player but in my project, it's on the enemy. I got it working until I needed to spawn in enemies. In order to do so, my enemies had to be prefabs. When I made them into prefabs, the health bar was no longer in the field for it in the inspector (10:12 in the video). It was giving me an error. I changed the health bar item into a prefab and I was able to put it into the field. It no longer gave me an error but it also wasn't changing the health bar. I don't expect anyone to go through the trouble of helping here but if you could, I would really appreciate it. If you need me to add the scripts I could just leave a comment or something I'm not entirely sure how Unity answers works either.
Answer by cortvi · Jun 08, 2020 at 10:14 PM
Try this:
void SpawnEnemy(Transform _enemy)
{
Debug.Log("Spawning Enemy:" + _enemy.name);
Transform _sp = spawnPoints[Random.Range(0, spawnPoints.Length)];
var e = Instantiate(_enemy, _sp.position, _sp.rotation).GetComponent<Enemy> ();
e.healthBar = this.healthBar;
}
Answer by lukittoburrito · Jun 06, 2020 at 11:02 PM
Enemy spawner
public enum SpawnState {SPAWNING, WAITING, COUNTING};
[System.Serializable]
public class Wave
{
public string name;
public Transform enemy;
public int count;
public float rate;
}
public Wave[] waves;
private int nextWave = 0;
public Transform[] spawnPoints;
public HealthBar healthBar;
public float timeBetweenWaves = 5f;
private float waveCountdown;
private float searchCountdown = 1f;
private SpawnState state = SpawnState.COUNTING;
void Start()
{
if (spawnPoints.Length == 0)
{
Debug.LogError("No Spawn Points Referenced");
}
waveCountdown = timeBetweenWaves;
}
void Update()
{
if (state == SpawnState.WAITING)
{
if (!EnemyIsAlive())
{
WaveCompleted();
}
else
{
return;
}
}
if (waveCountdown <= 0)
{
if (state != SpawnState.SPAWNING)
{
StartCoroutine(SpawnWave(waves[nextWave]));
}
}
else
{
waveCountdown -= Time.deltaTime;
}
}
void WaveCompleted()
{
Debug.Log("Wave Completed");
state = SpawnState.COUNTING;
waveCountdown = timeBetweenWaves;
if (nextWave + 1 > waves.Length - 1)
{
nextWave = 0;
Debug.Log("All Waves Completed. Looping...");
}
else
{
nextWave++;
}
}
bool EnemyIsAlive()
{
searchCountdown -= Time.deltaTime;
if (searchCountdown <= 0f)
{
searchCountdown = 1f;
if (GameObject.FindGameObjectWithTag("enemy") == null)
{
return false;
}
}
return true;
}
IEnumerator SpawnWave (Wave _wave)
{
Debug.Log("Spawning Wave..." + _wave.name);
state = SpawnState.SPAWNING;
for (int i = 0; i < _wave.count; i++)
{
SpawnEnemy(_wave.enemy);
yield return new WaitForSeconds(1f / _wave.rate);
}
state = SpawnState.WAITING;
yield break;
}
void SpawnEnemy(Transform _enemy)
{
Debug.Log("Spawning Enemy:" + _enemy.name);
Transform _sp = spawnPoints[Random.Range(0, spawnPoints.Length)];
Instantiate(_enemy, _sp.position, _sp.rotation);
}
}
enemy script
{
private float time = 0.0f;
public float damage = 2f;
public float attackSpeed = 2f;
public HealthBar healthBar;
Transform target;
void Start()
{
target = PlayerManager.instance.player.transform;
agent = GetComponent<NavMeshAgent>();
}
void Update()
{
float distance = Vector3.Distance(target.position, transform.position);
if (distance <= lookRadius)
{
agent.SetDestination(target.position);
if (distance <= agent.stoppingDistance)
{
FaceTarget();
}
}
time += Time.deltaTime;
if (time >= attackSpeed)
{
time = 0.0f;
if (distance <= agent.stoppingDistance && playerStats.baseHealth >= 0)
{
Attack();
}
}
}
void Attack()
{
Debug.Log(playerStats.currentHealth);
_ = playerStats.currentHealth -= damage;
healthBar.SetHealth(playerStats.currentHealth);
}
}
@cortvi OH $$anonymous$$Y GOD THANK YOU SO $$anonymous$$UCH I HAVE BEEN SEARCHING THE INTERNET FOR A LONG TI$$anonymous$$E LOOKING FOR THIS ANSWER. But seriously you are a god thx :)