Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

All Health bars changing at the same time when one is damaged.

Discussion in 'Scripting' started by guyanermanator, Apr 13, 2018.

  1. guyanermanator

    guyanermanator

    Joined:
    Mar 17, 2018
    Posts:
    13
    So I made a few health systems. One for enemies and one for the player. Each targeting different sliders and different named variables. but when one is changed they all change? Does anyone know what I am doing wrong? Here are the scripts.

    PlayerManager

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PlayerManager : MonoBehaviour {
    7.     public static int health = 100;
    8.     public GameObject Player;
    9.     public  Slider healthBar;
    10.     // Use this for initialization
    11.     void Start () {
    12.         InvokeRepeating ("ReduceHealth", 0,1);
    13.        
    14.     }
    15.     void ReduceHealth ()
    16.     {
    17.         health = health - 0;
    18.         healthBar.value = health;
    19.         if (health <= 0)
    20.             ;
    21.            
    22.        
    23.  
    24.     }
    25. }

    Health Item

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HealthItem : MonoBehaviour {
    6.     void OnTriggerEnter(Collider other)
    7.     {
    8.         PlayerManager.health = PlayerManager.health +30;
    9.         Destroy (this.gameObject);
    10.  
    11.     }
    12.     // Use this for initialization
    13.     void Start () {
    14.        
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.        
    20.     }
    21. }
    22.  



    Enemy Damage item

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class EnemyDamageItem : MonoBehaviour {
    7.     void OnTriggerEnter(Collider other)
    8.     {
    9.         EnemyManager.Enemyhealth = EnemyManager.Enemyhealth -30;
    10.  
    11.  
    12.     }
    13.     // Use this for initialization
    14.     void Start () {
    15.        
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.        
    21.     }
    22. }
    23.  


    Enemy Manager


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class EnemyManager : MonoBehaviour {
    7.     public static int Enemyhealth = 100;
    8.     public GameObject Enemy;
    9.     public static Slider EnemyhealthBar;
    10.     // Use this for initialization
    11.     void Start () {
    12.         InvokeRepeating ("ReduceEnemyhealth", 0,1);
    13.        
    14.     }
    15.     void ReduceEnemyhealth ()
    16.     {
    17.         Enemyhealth = Enemyhealth - 0;
    18.         EnemyhealthBar.value = Enemyhealth;
    19.         if (Enemyhealth <= 0)
    20.             ;
    21.            
    22.        
    23.  
    24.     }
    25. }
    26.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
    Your Enemyhealth is static...which means all scripts share the same value. You can't do static for something that needs to be tracked on each enemy.

    Static means it's not tied to an instance of the class. Which is why you can access it through the class.

    Code (CSharp):
    1. Enemy.Health = 100; //Access to Health through the static variable on Enemy
    2.  
    3. Enemy newEnemy = hitEnemy.GetComponent<Enemy>();
    4. newEnemy.Health = 100; //Access to an instance of the Enemy script on the hitEnemy gameobject.
     
    guyanermanator likes this.
  3. guyanermanator

    guyanermanator

    Joined:
    Mar 17, 2018
    Posts:
    13
    THANKS MAN !!!!!! I tried changing that static before but this opens so many doors ha. Thanks.
     
  4. guyanermanator

    guyanermanator

    Joined:
    Mar 17, 2018
    Posts:
    13
    Not really sure where to put what
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Did you try the example he posted?

    Getting the component on trigger enter and adjusting the health?