Health Bar (925391)

Is this the correct scripting format of the health bar or not?

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.

If you have an error you don’t understand… post the error. We’re not mind readers.

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.

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 @ pointed out, you have heath -= 2; yet have never defined a variable called health anywhere in your script.

What is ā€œSilderā€ and why is it not public, or at least ā€œgottenā€ in the Start() method?

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: Using code tags properly

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?

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:

public float maxHealth = 13;
public float curHealth;

Also you have another typo… In that script is curHealth, and in your error code it says currHealth

A: Use a proper IDE like visual studio that has proper Unity integration.
B: Learn how to code property: Junior Programmer Pathway - Unity Learn

These forums aren’t here to fix your typos.

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.