Question on GUI Text

How do I print out text on my screen , throughout the game for my healthbar? when I tried what was prescribed in the unity site it gave some errors. This is my code.

using UnityEngine;
using System.Collections;

public class Controls : MonoBehaviour {
float h=10f;
void Start () {
GUI.Box( Rect (10,10, 100,50), null);
}
void Update () {
if (Input.GetKey (KeyCode.UpArrow)) {
transform.Translate (Vector3.forward * Time.deltaTime * 10f);
}
else if(Input.GetKey (KeyCode.DownArrow)) {
transform.Rotate(Vector3.up,500f*Time.deltaTime);
}

}
}

The Errors I get
1.
Assets/Space Stuff/Controls.cs(7,26): error CS0119: Expression denotes a type', where a variable’, value' or method group’ was expected

Assets/Space Stuff/Controls.cs(7,21): error CS1502: The best overloaded method match for `UnityEngine.GUI.Box(UnityEngine.Rect, string)’ has some invalid arguments

Assets/Space Stuff/Controls.cs(7,21): error CS1503: Argument #1' cannot convert object’ expression to type `UnityEngine.Rect’

You miss a new operator for your GUI rectangle box which is mentioned in Line(7), Char(26). Furthermore, you will not see any GUI element ingame, as it needs to be placed in the OnGUI() method rather than in Start()! Read more about it here.

void OnGUI() {
    GUI.Box(new Rect (10,10, 100,50), "");
}

Please use proper code formatting for your future posts, as it makes reading much easier, see the insert option of your post window for more options.

Thank You , sir ! it worked. Its really awesome to have this Unity Forum where I get helped easily.

1 Like

I also encountered a similar problem, thank helped

1 Like