Today I tried to make the player’s health bar work. I created a function in the Player script to update health when it got enemy damage and called it in the Enemy script on collision.
This is all what I do today:
In the Enemy Script
In this function, it finds the GameObject with the tag “Player“. If it does not exist, print null.

Next, if the object collides with an enemy and it has been identified as the player, call the getEnemyDamage() function in the Player script. Otherwise, print null.

In the Player Script
In the Player script, create a function called getEnemyDamage() that takes Damage as an argument. When the player collides with an enemy, subtract the enemy’s damage from the player’s health. Then update and draw the player’s health bar to reflect the new health value.

However, I got an error: “Object reference not set to an instance an object”. I will try to fix it next time.

No Responses