Helllo. Im very new to unity and for some reason my if statement isnt working on “if currentHealth < 0)”.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using TMPro;
public class PlayerController : MonoBehaviour
{
///moveomento
public float moveSpeed;
static int moneyAmount;
Vector2 movement;
//mentioning stuff i think
public Rigidbody2D rb;
public Animator anim;
///coins
[SerializeField]
private TMP_Text coinCounter;
public static int collidedCoinValue;
public static int collidedCoinValue2;
///Health
public int maxHealth = 100;
public int currentHealth;
public HealthBarScript healthbar;
///Defeat and Victory
public GameObject deathScreen;
public AudioSource defeatMusic;
private bool dead;
void Start()
{
moneyAmount = 0;
currentHealth = maxHealth;
healthbar.SetMaxHealth(maxHealth);
}
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
anim.SetFloat("Horizontal", movement.x);
anim.SetFloat("Vertical", movement.y);
anim.SetFloat("Speed", movement.sqrMagnitude);
coinCounter.text = "" + moneyAmount;
}
void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.deltaTime);
}
private void OnTriggerEnter2D(Collider2D coll)
{
collidedCoinValue = coll.gameObject.GetComponent<CoinScript>().coinValue;
moneyAmount += collidedCoinValue;
Destroy(coll.gameObject);
}
void OnDisable()
{
PlayerPrefs.SetInt("moneyAmount", moneyAmount);
}
public void PlayerTakeDamage(int damage)
{
currentHealth -= damage;
healthbar.SetHealth(currentHealth);
if(currentHealth < 0)
{
KillPlayer();
}
void KillPlayer()
{
deathScreen.SetActive(true);
defeatMusic.enabled = true;
GetComponent<PlayerController>().enabled = false;
}
}
}
No errors no nothing. It just doesnt work. Any help?