- Home /
I am trying to interconnect these 5 scripts to get a functioning and working Health Bar that restarts when respawned and you respawn when it hits zero. Object reference not set to an instance of an object
Enemy Kill: public class EnemyKill : MonoBehaviour {
public int maxHealth = 100;
public int currentHealth;
public HealthBar healthBar;
public LevelManager levelManager;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
levelManager = FindObjectOfType<LevelManager>();
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.name == "Player")
{
TakeDamage(20);
}
}
void TakeDamage(int damage)
{
currentHealth -= damage;
healthBar.SetHealth(currentHealth);
if (currentHealth <= 0)
{
levelManager.RespawnPlayer();
}
}
public void HealthBarRestart()
{
healthBar.SetHealth(maxHealth);
}
} Enemy: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Enemy : MonoBehaviour {
public int health = 100;
public LevelManager levelManager;
public GameObject deathEffect;
public void Start()
{
levelManager = FindObjectOfType<LevelManager>();
}
public void TakeDamage(int damage)
{
health -= damage;
if (health <= 0)
{
Die();
levelManager.RestartHealth();
}
}
void Die()
{
Instantiate(deathEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
} HealthBar: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class HealthBar : MonoBehaviour {
public Slider slider;
public void SetMaxHealth(int health)
{
slider.maxValue = health;
slider.value = health;
}
public void SetHealth(int health)
{
slider.value = health;
}
} Player Combat: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerCombat : MonoBehaviour {
public Animator animator;
public Transform attackPoint;
public float attackRange = 0.5f;
public LayerMask enemyLayers;
public float attackRate = 2f;
float nextAttackTime = 0f;
void Update()
{
if(Time.time >= nextAttackTime)
{
if (Input.GetKeyDown(KeyCode.E))
{
Attack();
nextAttackTime = Time.time + 1f / attackRate;
}
}
}
void Attack()
{
animator.SetTrigger("Attack");
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);
foreach(Collider2D enemy in hitEnemies)
{
enemy.GetComponent<Enemy>().TakeDamage(100);
}
}
void OnDrawGizmosSelected()
{
if (attackPoint == null)
return;
Gizmos.DrawWireSphere(attackPoint.position, attackRange);
}
} And Level Manager: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class LevelManager : MonoBehaviour { public GameObject currentCheckpoint;
private PlayerController player;
public EnemyKill enemy;
public GameObject deathParticle;
public GameObject respawnParticle;
// Start is called before the first frame update
public void Start()
{
player = FindObjectOfType<PlayerController>();
enemy = FindObjectOfType<EnemyKill>();
}
// Update is called once per frame
public void RestartHealth()
{
enemy.HealthBarRestart();
}
public void RespawnPlayer()
{
Instantiate(deathParticle, player.transform.position, player.transform.rotation);
Debug.Log("Player Respawn");
player.transform.position = currentCheckpoint.transform.position;
Instantiate(respawnParticle, currentCheckpoint.transform.position, currentCheckpoint.transform.rotation);
}
}
Your answer
Follow this Question
Related Questions
Orthello2D sprites "lose" spriteIndex after testplaying 0 Answers
HDR spheremap not appearing in scene 0 Answers
Mouse Wrapping Near Edge of Play Space 0 Answers
Scene loading time after upgrade from 5.5 to 5.6 4 Answers
Inspector issue about draw Button(s) on top of EditorGUI fields, and "CLICK" on it. 1 Answer