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 bevoid 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 calledhealthanywhere 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:
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.
