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. Dismiss Notice

Question Health Bar

Discussion in 'Getting Started' started by dda846, Aug 4, 2023.

  1. dda846

    dda846

    Joined:
    Apr 1, 2023
    Posts:
    12
    Is this the correct scripting format of the health bar or not?

    Screenshot 2023-08-03 221940.png
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,856
    What does "correct" mean here? Does it work, or doesn't it work?

    Surely you can determine the answer yourself by just testing your code.
     
    Joe-Censored likes this.
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,856
    If you have an error you don't understand... post the error. We're not mind readers.
     
    Joe-Censored likes this.
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,904
    Well, it's more specific than you are. You included a screenshot of a class instead of using the CODE tags as it is requirement on the forums. You can find help here, how to use CODE tags.
    You also failed to include the error message in your OP so we can only guess what the problem is.
    In your screenshot, on the 23rd line, you have a command
    health -= 2;
    and then you check it on the 26th. But there is no declaration of
    health
    variable anywhere, you have
    currHealth
    instead.
     
    spiney199 likes this.
  5. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    488
    I can see 2 glaring errors in your script, but it would have been helpful if you had given us the error message instead of just a post containing your script (and not even mention in your original post that there was an error).

    - You have put
    void On TriggerStay2D(collider2D col)
    when it should be
    void OnTriggerStay2D(collider2D col)
    . Note the fact that your had incorrectly put a space in the method name.
    - Just as @Lurking-Ninja pointed out, you have
    heath -= 2;
    yet have never defined a variable called
    health
    anywhere in your script.
     
  6. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    What is "Silder" and why is it not public, or at least "gotten" in the Start() method?
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    You're just getting a nullref. Go fix it. You don't need to post here because...

    The answer is always the same... ALWAYS!

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that

    After that, as others have pointed out, remember that photographs of code are NOT A THING.

    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/

    Also, making people guess "is this the correct format?" is also not useful.

    Here is how to report technical problems to complete strangers on the internet:

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?
     
    Joe-Censored likes this.
  8. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Just so you're aware, you have tons of issues.. they will pop up after you fix that current issue. You need to make the variables public:
    Code (CSharp):
    1. public float maxHealth = 13;
    2. public float curHealth;
    Also you have another typo.. In that script is curHealth, and in your error code it says currHealth
     
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,856
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,511
    With all due respect, please try to put more effort into finding your own typos. The forums are here to help with problems but are not here for you to learn the basics of how to type the correct basic syntax of the C# language.

    Whilst some devs will help you by telling you that you missed a space here or didn't use correct casing there, they are not here to guide you through each and every typo.

    The forums assume a basic understanding of the C# language and how to construct it and this includes understanding basic compiler errors, after all, most give you a description and you can easily look up that error code. There's a huge amount of self-help material online in terms of tutorials/documents at your fingertips so there should be no reason to use these forums for basic typos one after the other.

    Don't be put off asking on the forums but also, as a starter, please consider using the Getting Started forum and following the Learn material it links to.

    I'll move this post to the Getting Started forum.

    Thanks.
     
    Joe-Censored likes this.