public class attack : MonoBehaviour
{
public Animator animator;
public Transform AttackPoint;
public float attackRange = 0.5f;
public LayerMask enemyLayers;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.RightControl))
{
// Update is called once per frame
void Update()
{
{
if (DoHealth == 2)
{
animator.SetBool(“Health10”, true);
}
}
}}
I want it so that when the attack is triggered (foreach collider) it adds one to the DoHealth in HealthCode, so i can add a new animation to the sprite.
Referencing GameObjects, Scripts, variables, fields, methods (anything non-static) in other script instances or GameObjects:
It isn’t always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.
Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.
That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.