- Home /
Health System
my health system isn t working and i don t know what to do
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class playerHealth : MonoBehaviour { public float fullHealth;
float currentHealth;
// Start is called before the first frame update
void Start()
{
currentHealth = fullHealth;
}
// Update is called once per frame
void Update()
{
if (currentHealth -= 0);
Destroy(GameObject);
}
}
Answer by Marker_R · Dec 02, 2019 at 07:48 PM
Firs of all you need to make if currentHealth <= 0. And if you can please send the full script so i can see the other problems.
Answer by developeration1 · Dec 02, 2019 at 08:28 PM
The errors come from the inside of the Update. For the first error, at the if (currentHealth -= 0);
you have a logic and syntax error, I tell you in order for you to understand how if statements work:
The
-=
its a Subtract AND assignment operator, it subtracts right operand from the left operand and assign the result to left operand. If what you are trying to do is check if thecurrentHealth
is less or equal to 0, then the operation would becurrentHealth <= 0
.The
if
statement syntax normally shouldn't have a semicolon (;
) at the end, its normal syntax would be:
.
if ( <condition> )
{
//If the condition is true, then make what is inside the { }.
}
For last, the second error that you are getting it's because inside the
Destroy()
you are referring to your gameObject wrong, it can be confussing but let me explain: When you useGameObject
with G you are talking about the GameObject type, and when you usegameObject
with g you are talking about the actual Game Object you are using, so it should beDestroy (gameObject)
.
Taking all this fixes, your if
statement should look like:
if (currentHealth <= 0)
{
Destroy (gameObject);
}
In order to help you understand better the logic and syntax of C#, I recommend you to check out the C# tutorial at tutorialspoint.com, which I'll leave you here: https://www.tutorialspoint.com/csharp/index.htm Hope that helps you out!
Your answer
Follow this Question
Related Questions
2D in Unity3D 2 Answers
2d Isometric game? 1 Answer
what is the best way to do this game? 2 Answers
8-directional orientation, top down 2D, seperate from movement 0 Answers
Clear, Crisp pixel textures for a 2D RPG 2 Answers