- Home /
Health UI Display
I'm setting up the UI to the health code
void OnGUI() {
GUI.Label(new Rect(10, 10, 700, 40), "Health" + GetComponent<Health> Health);
}
This is a separate script
#pragma strict
var MaxHealth = 100;
var Health : int;
function Start ()
{
Health = MaxHealth;
}
function ApplyDammage (TheDammage : int)
{
Health -= TheDammage;
if(Health <= 0)
{
Dead();
}
}
function Dead()
{
RespawnMenuV2.playerIsDead = true; //VERY IMPORTANT! This line was added in tutorial number 19. If you haven't reached that tutorial yet, go ahead and remove it.
Debug.Log("Player Died");
}
function RespawnStats ()
{
Health = MaxHealth;
}
But there's an error : error CS1525: Unexpected symbol `Health' How can I fix that?
Answer by HarshadK · Nov 05, 2014 at 07:17 AM
You are missing a '.' in your GUI.Label before your variable Health. That line of code should be:
GUI.Label(new Rect(10, 10, 700, 40), "Health" + gameObject.GetComponent<Health>().Health);
Also just to make sure, you do know you are using a JS script (Health Script) and a C# script (The one where you are displaying the Health).
And also its better to cache the component reference rather than using GetComponent() every single frame.
Answer by yoieyo · Nov 05, 2014 at 07:38 AM
You can also add a GUIText, you should put this script onto your player, then put the Health text as a variable: Such as this
var HealthCounter : GUIText;
function OnGUI () { HealthCounter.text = "Health: " + Health.ToString(); }
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Health Bar Messages 3 Answers
How can I create a UI element and attach my script to it for a functional health bar? 1 Answer