I’m having this problem for a while and can’t solve it: I keep getting this error at line 20 and 38, and the health_Bar functions keep failing.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
Animator animator;
float maxhealth = 20;
float health = 20;
[SerializeField] private Health_Bar health_Bar;
[SerializeField] float speed;
[SerializeField] float range;
[SerializeField] float maxdist;
Vector2 waypoint;
SpriteRenderer spriteRenderer;
public void Health(float damage) {
health -= damage;
health_Bar.UpdateHealth(health, maxhealth);
if (health <= 0) {
Defeated();
}
}
public void Defeated() {
animator.SetTrigger("defeat");
}
public void Removeenemy() {
Destroy(gameObject);
}
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
health_Bar = GetComponentInChildren<Health_Bar>();
SetNewDestination();
spriteRenderer = GetComponent<SpriteRenderer>();
health_Bar.UpdateHealth(health, maxhealth);
}
As you can see, the problem is with "health_Bar.something". I can’t figure out this problem and tried everything I can imagine and kept getting the same error.
What is the explanation for this error?