Hi everyone, so im making a game and when my player is hit by a bullet, his health will decrease. However, right now there is an error between my bullet script and my PlayerHP script
Assets\Script\BulletMovementTest.cs(53,13): error CS0120: An object reference is required for the non-static field, method, or property 'PlayerHP.health'
Bullet script
void OnTriggerEnter2D(Collider2D collider)
{
if (collider.gameObject.tag == "Player")
{
PlayerHP.health -= 1;
gameObject.SetActive(false);
Instantiate(impactEffect, transform.position, transform.rotation);
Debug.Log("hit");
}
PlayerHP
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerHP : MonoBehaviour
{
public int health;
public int numOfHearts;
public Image[] hearts;
public Sprite fullHeart;
public Sprite emptyHeart;
void Update()
{
if (health > numOfHearts)
{
health = numOfHearts;
}
for (int i = 0; i < hearts.Length; i++)
{
if (i < health)
{
hearts[i].sprite = fullHeart;
}
else
{
hearts[i].sprite = emptyHeart;
}
if (i < numOfHearts)
{
hearts[i].enabled = true;
}
else
{
hearts[i].enabled = false;
}
}
}
}
Would appreciate it if anyone can tell me how to resolve this error!
↧