How do you fix the error in this code.
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
public int maxHealth = 100;
public int curHealth = 100;
public float healthBarLenght;
void Start. () {
healthBarlenght = Screen.width / 2;
}
void Update () {
AddjustCurrentHealth(0);
}
void OnGui() {
GUI.Box(new Rect(10,10, healthBarLenght, 20), curHealth + "/" + maxHealth);
public void AddjustCurrentHealth(int adj) {
curHealth += adj;
if(curHealth < 0)
curHealth = 0;
if(curHealth > maxHealth)
curhealth = maxHealth;
if(maxHealth > 1)
maxHealth = 1;
healthBarLenght = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
}
Your AddjustCurrentHealth method is in another method and should nest inside the class in this case.
Edit: also there was a period after ‘Start’. Also OnGUI was entered incorrectly
using System.Collections;
using UnityEngine;
public class PlayerHealth : MonoBehaviour {
public int maxHealth = 100;
public int curHealth = 100;
public float healthBarLenght;
void Start () {
healthBarlenght = Screen.width / 2;
}
void Update () {
AddjustCurrentHealth(0);
}
void OnGUI() {
GUI.Box(new Rect(10,10, healthBarLenght, 20), curHealth + "/" + maxHealth);
}
public void AddjustCurrentHealth(int adj) {
curHealth += adj;
if(curHealth < 0)
curHealth = 0;
if(curHealth > maxHealth)
curhealth = maxHealth;
if(maxHealth > 1)
maxHealth = 1;
healthBarLenght = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
}
I can change currentHealth and maxHealth in the inspector.But it won’t change in gameplaymode.