Search Unity

Newbie needing help with health script???

Discussion in 'Scripting' started by lycoriis, Dec 8, 2018.

  1. lycoriis

    lycoriis

    Joined:
    Dec 8, 2018
    Posts:
    3
    Hey guys!! I've been taking a game development class for college and I'm not exactly....the best at scripting haha! I'm trying to complete a simple health system for my game (when enemy attacks player, health is deducted, player death when health is 0) and i'm getting the errors:

    Assets/Scripts/EnemyAttack.cs(50,20): error CS1061: Type `HealthManager' does not contain a definition for `currentHealth' and no extension method `currentHealth' of type `HealthManager' could be found. Are you missing an assembly reference?
    and
    Assets/Scripts/EnemyAttack.cs(51,18): error CS1061: Type `HealthManager' does not contain a definition for `TakeDamage' and no extension method `TakeDamage' of type `HealthManager' could be found. Are you missing an assembly reference?

    on these scripts...

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class HealthManager : MonoBehaviour {
    6.  
    7.     public int startingHealth = 10; //starting health
    8.  
    9.     public static int currentHealth; //current health
    10.  
    11.     Text HealthText;
    12.    
    13.     public static int maxHealth= 10; //health cap
    14.  
    15.     //references
    16.     CharacterMove characterMove;
    17.     PlayerShoot playerShoot;
    18.  
    19.     bool isDead; //death
    20.     bool damaged; //take damage = true
    21.  
    22.     void Awake (){
    23.  
    24.         characterMove = GetComponent <CharacterMove> ();
    25.         playerShoot = GetComponentInChildren <PlayerShoot>();
    26.  
    27.         //initial health
    28.         currentHealth = startingHealth;
    29.     }
    30.  
    31.     void Start () {
    32.  
    33.         HealthText = GetComponent<Text>();
    34.  
    35.         currentHealth = 10;
    36.  
    37.         maxHealth = 10;
    38.     }
    39.        
    40.        
    41.     void Update () {
    42.  
    43.         //health
    44.  
    45.         if (currentHealth <= 0){
    46.             currentHealth = 0;
    47.             HealthText.text = " " + currentHealth;
    48.            
    49.         }
    50.        
    51.         if(currentHealth >= 10){
    52.             currentHealth = 10;
    53.             HealthText.text = " " + Health;
    54.         }
    55.     }
    56.  
    57.     public void TakeDamage (int amount){
    58.        
    59.         damaged = true;
    60.  
    61.         //reduction
    62.         currentHealth -= amount;
    63.  
    64.         //death
    65.         if(currentHealth <= 0 && !isDead){
    66.             //death
    67.             Death();
    68.         }
    69.     }
    70.  
    71.     void Death (){
    72.         //non repeat function
    73.         isDead = true;
    74.         //disable functions
    75.         playerShoot.DisableEffects();
    76.         playerMovement.enabled = false;
    77.         playerShoot.enabled = false;
    78.  
    79.     }
    80. }
    81.  
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class EnemyAttack : MonoBehaviour {
    5.  
    6.     public float attackWait = 1.0f; //interval
    7.     public int attackDamage = 2;
    8.  
    9.     GameObject player;
    10.     HealthManager healthManager; //PC Health
    11.     bool playerInRange; //is nearby?
    12.     float timer; //time before next attack
    13.  
    14.  
    15.     void Awake (){
    16.         //references
    17.         player = GameObject.FindGameObjectWithTag ("Player");
    18.         healthManager = player.GetComponent <HealthManager> ();
    19.     }
    20.  
    21.     void OnTriggerEnter (Collider2D other){
    22.         //Player collider
    23.         if(other.gameObject == player){
    24.             //if in range
    25.             playerInRange = true;
    26.         }
    27.     }
    28.  
    29.     void OnTriggerExit (Collider2D other){
    30.         //Player collider
    31.         if(other.gameObject == player){
    32.             //if not in range
    33.             playerInRange = false;
    34.         }
    35.     }
    36.    
    37.     // Update is called once per frame
    38.     void Update () {
    39.         //Add the time since Update was last called
    40.         timer += Time.deltaTime;
    41.  
    42.         if(timer >= attackWait && playerInRange){
    43.             Attack();
    44.         }
    45.     }
    46.  
    47.     void Attack (){
    48.         timer = 0f;
    49.  
    50.         if(healthManager.currentHealth > 0){
    51.             healthManager.TakeDamage (attackDamage);
    52.         }
    53.     }
    54. }
    55.  
    To me it looks like the Enemy Attack Script somehow can't find the Health Manager script??
    I know this is a lot of text, but I'm hoping someone can help me out! My final is due Sunday afternoon so I'm trying to crank this game out lolol. Unfortunately, I'm still super new at this and can't spot the error.

    Any help or advice is appreciated...thank you in advance!!
     
  2. Static member variables are global, usually you should not use them. But if you want, you can access them on the class and not on the instance objects. So HealthManager.currentHealth would work (although I advise you to remove the static from the variable definition instead on the line 9 of the HealthManager script so you can access it normally). The same problem will arise with the maxHealth as well.
     
    lycoriis likes this.
  3. lycoriis

    lycoriis

    Joined:
    Dec 8, 2018
    Posts:
    3
    Thank you so so much!!! Your explanation really helped me out! I fixed the static lines and a few other errors as well! :D
     
    Lurking-Ninja likes this.
  4. lycoriis

    lycoriis

    Joined:
    Dec 8, 2018
    Posts:
    3
    @LurkingNinjaDev I changed some more things around and suddenly this is coming up :( !
    Here are all of my health scripts below! I tried changing if certain objects were static or not and I got the error regardless (instead "an object ref is required to access static...etc.")

    Where do you think the problem is??

    Assets/Scripts/EnemyAttack.cs(50,20): error CS0120: An object reference is required to access non-static member `HealthManager.Health'

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class HealthManager : MonoBehaviour {
    6.  
    7.     public int startingHealth = 10; //starting health
    8.  
    9.     public int Health; //current health
    10.  
    11.     Text HealthText;
    12.    
    13.     public int maxHealth = 10; //health cap
    14.  
    15.     //references
    16.     CharacterMove characterMove;
    17.     PlayerShoot playerShoot;
    18.  
    19.     bool isDead; //death
    20.     bool damaged; //take damage = true
    21.  
    22.     void Awake (){
    23.  
    24.         characterMove = GetComponent <CharacterMove> ();
    25.         playerShoot = GetComponentInChildren <PlayerShoot>();
    26.  
    27.         //initial health
    28.         Health = startingHealth;
    29.     }
    30.  
    31.     void Start () {
    32.  
    33.         HealthText = GetComponent<Text>();
    34.  
    35.         Health = 10;
    36.  
    37.         maxHealth = 10;
    38.     }
    39.        
    40.        
    41.     void Update () {
    42.  
    43.         //health
    44.  
    45.         if (Health <= 0){
    46.             Health = 0;
    47.             HealthText.text = " " + Health;
    48.            
    49.         }
    50.        
    51.         if(Health >= 10){
    52.             Health = 10;
    53.             HealthText.text = " " +Health;
    54.         }
    55.     }
    56.  
    57.     public void TakeDamage (int amount){
    58.        
    59.         damaged = true;
    60.  
    61.         //reduction
    62.         Health -= amount;
    63.  
    64.         //death
    65.         if(Health <= 0 && !isDead){
    66.             //death
    67.             Death();
    68.         }
    69.     }
    70.  
    71.     void Death (){
    72.         //non repeat function
    73.         isDead = true;
    74.         //disable functions
    75.    
    76.         characterMove.enabled = false;
    77.         playerShoot.enabled = false;
    78.  
    79.     }
    80.  
    81.     public static void AddHealth (int HealthToAdd) {
    82.         Health += HealthToAdd;
    83.     }
    84. }
    85.  
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class EnemyAttack : MonoBehaviour {
    5.  
    6.     public float attackWait = 1.0f; //interval
    7.     public int attackDamage = 2;
    8.  
    9.     GameObject player;
    10.     HealthManager healthManager; //PC Health
    11.     bool playerInRange; //is nearby?
    12.     float timer; //time before next attack
    13.  
    14.  
    15.     void Awake (){
    16.         //references
    17.         player = GameObject.FindGameObjectWithTag ("Player");
    18.         healthManager = player.GetComponent <HealthManager> ();
    19.     }
    20.  
    21.     void OnTriggerEnter (Collider other){
    22.         //Player collider
    23.         if(other.gameObject == player){
    24.             //if in range
    25.             playerInRange = true;
    26.         }
    27.     }
    28.  
    29.     void OnTriggerExit (Collider other){
    30.         //Player collider
    31.         if(other.gameObject == player){
    32.             //if not in range
    33.             playerInRange = false;
    34.         }
    35.     }
    36.    
    37.     // Update is called once per frame
    38.     void Update () {
    39.         //Add the time since Update was last called
    40.         timer += Time.deltaTime;
    41.  
    42.         if(timer >= attackWait && playerInRange){
    43.             Attack();
    44.         }
    45.     }
    46.  
    47.     void Attack (){
    48.         timer = 0f;
    49.  
    50.         if(HealthManager.Health > 0){
    51.             HealthManager.TakeDamage (attackDamage);
    52.         }
    53.     }
    54. }
    55.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HealthPickup : MonoBehaviour {
    5.  
    6.     public int HealthToAdd;
    7.  
    8.     void OnTriggerEnter2D (Collider2D Other){
    9.         if (Other.GetComponent<Rigidbody2D> () == null)
    10.             return;
    11.  
    12.         HealthManager.AddHealth (HealthToAdd);
    13.  
    14.         Destroy (gameObject);
    15.     }
    16. }
    17.  
    18.