I am just starting with unity and c#. I have some previous code experience (True Basic and HTML). The way I learned these was just by doing examples, writing up the pseudocode and then trying to do it myself. Then I would try and fix the errors. K some things I can’t figure out for the code here that I have made. Don’t laugh. Again I am not brilliant and have just started
using UnityEngine;
using System.Collections;
public class Players_Health : MonoBehaviour{
public int MaxHealth = 100;
public int CurrentHealth = 100;
public float HealthBarLegth;
// program to be assigned to the player.
// first time at trying to write a c#.
Void start () {
HealthBarLength = screen.width / 2;
}
Void update () {
If (CurrentHealth > MaxHealth);
CurrentHealth = MaxHealth;
}
If (CurrentHealth == 0 || CurrentHealth >> 0)
{
Destroy(player)
}
Void OnGUI () {
GUI.Box (10,10,HealthBarLength, 20), CurrentHealth + "/" + MaxHealth);
}
void/if shouldn’t be capitalised, but Start/Update should.
Also, line 15 isn’t doing anything. It shouldn’t have a “;” after it, and line 17 should be inside “{” and “}”. Line 20 isn’t contained inside a function.
OnGUI is missing the second “}” among other issues such as “f” following floats.
Here’s a cleaned up version:
using UnityEngine;
using System.Collections;
public class Players_Health : MonoBehaviour {
[Header("Health Options")]
public int maxHealth = 100;
public int currentHealth = 100;
[Header("GUI Options")]
public float healthBarWidth;
void Start()
{
healthBarWidth = Screen.width / 2f;
}
void Update()
{
if (currentHealth > maxHealth)
currentHealth = maxHealth;
if (currentHealth == 0 || currentHealth < 0)
{
Destroy(gameObject);
}
}
void OnGUI ()
{
GUI.Box(new Rect(10f, 10f, healthBarWidth, 20f), currentHealth + "/" + maxHealth);
}
}
It looks like line 20 should be inside of a function.
Move the bracket on line 18 to line 24
Also line 20 CurrentHealth >> 0. this is a bit shift that does nothing, looks like you need CurrentHealth > 0. Although would you want to destroy the player when their health is greater than 0?