Search Unity

Question health bar code not working

Discussion in 'Scripting' started by sadi1, Oct 30, 2020.

  1. sadi1

    sadi1

    Joined:
    Sep 16, 2020
    Posts:
    15
    i wrote this code (more like 10% of it) and its not working, i tried the original code but it was full of errors maybe cuz its old or idk, i fixed some stuff in it yet it still has one error that i cant fix

    error:
    Assets\Health.cs(17,13): error CS1061: 'HealthBar' does not contain a definition for 'SetMaxHealth' and no accessible extension method 'SetMaxHealth' accepting a first argument of type 'HealthBar' could be found (are you missing a using directive or an assembly reference?)


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.  
    8.     public int maxHealth = 100;
    9.     public int currentHealth;
    10.  
    11.     public HealthBar healthBar;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         currentHealth = maxHealth;
    17.         healthBar.SetMaxHealth(maxHealth);
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         if (Input.GetKeyDown(KeyCode.Space))
    24.         {
    25.             TakeDamage(20);
    26.         }
    27.     }
    28.  
    29.     void TakeDamage(int damage)
    30.     {
    31.         currentHealth -= damage;
    32.  
    33.         healthBar.SetHealth(currentHealth);
    34.     }
    35. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Look at line 17 and look at line 33.
     
    sadi1 likes this.
  3. sadi1

    sadi1

    Joined:
    Sep 16, 2020
    Posts:
    15
    what is wrong with them?
     
    K1327399 likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Is the function called
    SetHealth
    (line 33) or is the function called
    SetMaxHealth
    (line 17)?

    If the compiler is complaining, show me the code for the function you think is there.
     
    sadi1 likes this.
  5. abhilashgajbhiye77

    abhilashgajbhiye77

    Joined:
    Dec 27, 2020
    Posts:
    2
    same error
     
  6. abhilashgajbhiye77

    abhilashgajbhiye77

    Joined:
    Dec 27, 2020
    Posts:
    2
    with those two lines only
     
  7. AbandonedCrypt

    AbandonedCrypt

    Joined:
    Apr 13, 2019
    Posts:
    73
    I'm sorry but you should probably learn the very basics of programming before attempting to write a game. There's nothing wrong with using game development to depthen a basic understanding of programming, but there should be a base line of knowledge present to not run into issues like this.

    It is very clear from the error message, that
    SetMaxHealth
    (Line 17) does not exist or is not accessible.

    That means your class HealthBar does not have a (public) method
    SetMaxHealth
    .
     
    Lekret and Kurt-Dekker like this.
  8. NovacerSuanah

    NovacerSuanah

    Joined:
    May 25, 2021
    Posts:
    1
    Sadi1 and abhilashgajbhiy77, make sure you're paying attention to capital and lowercase letters. C# is sensitive to that. If you got this code from Brackeys, make sure that both "SetMaxHealth" and "SetHealth" match completely with the code you wrote in your HealthBar component. You could just need to change it to "setMaxHealth" and "setHealth".
    If this isn't the case then you need to identify the methods before you can use them, whether that's listing them outside of your methods or getting them from another component.

    For the other people 'trying' to help but just being demeaning to new programmers, please understand that not everyone learns the same way. Some people have a much easier time learning to code when they have a goal in mind and can see what they're doing to accomplish that goal. "Learning the basics of programming before attempting to write a game" is not an appropriate method or advice for everyone. If you have no interest in helping people solve their programming issues and just want to be rude and/or repeat the problem without explaining it, please refrain from commenting.
     
    tyed604 and K1327399 like this.
  9. That's true, but it is irrelevant here. We aren't here to teach to code anyone.
    That's why courses have goals in them and various challenges. Use them, that's why they are available, both paid (usually better) or for free.
    It is. You shouldn't try to build a car before you learn how to use a screwdriver either. Obviously you can try, but you will fail miserably. It's a great advice, actually, but no one will hold gun at your head to accept and follow the advice.
    Woha, and you choose this to be your first post on these forums... necroing a post from more than 6 months ago, the op wasn't here (on the entire forum) more than two...
    Please read this: https://forum.unity.com/threads/unity-community-code-of-conduct.743180/

    Welcome to the Unity community forums.
     
    Last edited by a moderator: May 25, 2021
  10. true_warlock

    true_warlock

    Joined:
    Aug 2, 2019
    Posts:
    60
    I am having similar issues in LTS version 2020.3.32.f1.

    Health bar does not go down to zero til after enemy is destroyed. I use a bullet with an explosion to set off when it hits the enemy and using a slider. It seems there is a problem with the Engine itself because I do not get errors.

    Here are the simple example codes.

    For the Bullet which works as its suppose to:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class bullet : MonoBehaviour
    4. {
    5.     [SerializeField] [Range(1, 5)] private float lifeTime = 5.0f;
    6.     [Range(1, 5)] public Vector3 hitPoint;
    7.     public GameObject colExplode;
    8.  
    9.  
    10.     private void Awake()
    11.     {
    12.         Destroy(gameObject, lifeTime);
    13.     }
    14.     private void OnTriggerEnter(Collider col)
    15.     {
    16.  
    17.         if (col.gameObject.tag == "Enemy")
    18.         {
    19.             col.gameObject.GetComponent<Health>().currentHealth -= 20;
    20.             GameObject explosion = Instantiate(colExplode, transform.position, transform.rotation);
    21.             Destroy(gameObject);
    22.             Destroy(explosion, 5f);
    23.            
    24.         }
    25.        
    26.     }
    27. }
    and for the health that only destroys the player and makes the slider go to zero after object is destroyed:

    Code (CSharp):
    1. using UnityEngine.UI;
    2. public class Health : MonoBehaviour
    3. {
    4.     //Still doesn't have any effect on slider
    5.     [Range(0, 100)] public int currentHealth;
    6.     public Slider healthBar;
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.      
    11.     }
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.    
    16.         if (currentHealth <= 0)
    17.         {
    18.                Destroy(gameObject);
    19.         }
    20.         healthBar.value = currentHealth;
    21.     }
    22. }
    23.  
    It should all be working but its not and I have deleted literally everything, started a new project. For whatever reason it just refuses to work despite the fact there are no errors, slider is placed in script slot, and everything else is working correctly. I have tried several different ways of coding the health and the results are the same.
     
  11. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    348
    Probably best start your own thread with this. OP's issue was different :)

    Yes, it will do that because at minimum you're setting the healthBar.value on line 20 after you destroy the object on line 18. Sounds like maybe you want to move line 20 to line 15.

    If that's not what you mean, when you post can you be more specific with what you expect to happen.
     
  12. true_warlock

    true_warlock

    Joined:
    Aug 2, 2019
    Posts:
    60

    Yes, well I got tired of dealing with the slider and use an image instead. Only problem is when I actually start the game (and it only recently started having a problem with it, the healthbar ceases to exist in the script attacked to either player or target enemy, hazard, etc. Just for sake of argument here is the script.

    I really don't know what else to do at this point having tried so many work arounds.

    Code (CSharp):
    1.     [Range(0, 100)]  public int currentHealth = 100;
    2.      public int maxHealth = 100;
    3.  
    4.     public Image healthBar;
    5.     // Start is called before the first frame update
    6.     private void Start()
    7.     {
    8.         healthBar = GetComponent<Image>();
    9.     }
    10.     // Update is called once per frame
    11.     private void Update()
    12.     {
    13.  
    14.         healthBar.fillAmount = (float)currentHealth / (float)maxHealth;
    15.         if (healthBar != null)
    16.         {
    17.             healthBar.fillAmount = (float)currentHealth / (float)maxHealth;
    18.            
    19.         }
    20.         if (currentHealth <= 0)
    21.         {
    22.             Destroy(gameObject);
    23.         }
    24.     }
    25. }
    26.  

    I always get the error "not set to an instance, (yadda yadda) despite having done so. It only becomes worse if i try making enemies spawn.
     
  13. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    348
    Last edited: Apr 2, 2022
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
  15. true_warlock

    true_warlock

    Joined:
    Aug 2, 2019
    Posts:
    60
    Thanks for the assist and info.

    It turned out to be an internal bug in the version of the engine.

    I had to uninstall it and removed all the %AppData% and %ProgramData% folders associated with the buggy one before downloading the replacement updated one.

    Turned out the bug was specifically related to an issue with the UI feature which is now no longer an issue and was probably corrupted during the original download.
     
    Last edited: Apr 28, 2022
    K1327399 likes this.