'PlayerHealth': member names cannot be the same as their enclosing type error

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerHealth : MonoBehaviour {
private float PlayerHealth = 0f;
[SerializeField] private float maxHealth = 100f;

private void Start() {
health = maxHealth;
}

public void UpdateHealth(float mod) {
health += mod;

if (health > maxHealth) {
health = maxHealth;
}else if (health <= 0f) {
health = 0f;
Debug.Log(“Player Respawn”);
}
}
}

Want to put any less effort into a post?

The error tells you exactly the issue.

2 Likes

private float PlayerHealth = 0f;
You can’t name variables the same name as the class. Changing private float PlayerHealth = 0f to private float health = 0f; would remove that problem.

1 Like