Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question I need help accessing my float between 2 scripts

Discussion in 'Scripting' started by lorydambell, May 14, 2024.

  1. lorydambell

    lorydambell

    Joined:
    Nov 14, 2023
    Posts:
    1
    I have 2 scripts, one is for attacking:

    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))
    {

    Attack ();
    }
    }
    void Attack()
    {
    animator.SetTrigger("Attack");

    Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(AttackPoint.position, attackRange, enemyLayers);

    foreach(Collider2D enemy in hitEnemies)
    {
    Debug.Log("We hit" + enemy.name);
    }
    }

    And one is for the healthbar:

    public class HealthCode : MonoBehaviour
    {
    public Animator animator;
    public static float DoHealth = 1;

    bool Health1;
    bool Health2;
    bool Health3;
    bool Health4;
    bool Health5;
    bool Health6;
    bool Health7;
    bool Health8;
    bool Health9;
    bool Health10;
    bool Dead;


    // 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.

    Please help!
    Thanks,
    MrKatz
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,366
    Referencing GameObjects, Scripts, variables, fields, methods (anything non-static) in other script instances or GameObjects:

    https://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639

    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    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.


    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/


    If you're unsure how to proceed, try this approach:

    Imphenzia: How Did I Learn To Make Games: