- Home /
OnGUI() not showing anything
Hello! I'm quite new to Unity and game making in general. I'm trying to show a flat background at the left side of the screen and then make a health bar appear on it. But none of them show up. I've never used OnGUI() before and I'm not sure I got everything right. What I did was write a script which I'll post below and attach it to a new GameObject in the scene. This object has no other components. I dragged some images (I changed the "texture type" from sprite to texture) into the texture spots in this script in the inspector. Why does it not work? There are no errors nor warnings. The game runs properly. Please, tell me what I should do! Thanks :)
SCRIPT:
using UnityEngine;
using System.Collections;
public class HealthDisplay : MonoBehaviour {
private Vector2 pos = new Vector2 (0, 0);
private Vector2 size = new Vector2 (0, 0);
private Vector2 barPos = new Vector2 (0, 0);
private Vector2 barSize = new Vector2 (0, 0);
private float percentage = 1f;
public Texture emptyTex;
public Texture fullTex;
public Texture bgTex;
ScreenBoundsScript screenBounds;
// Use this for initialization
void Start () {
screenBounds = GameObject.Find("Manager").GetComponent<ScreenBoundsScript>();
size = new Vector2 (screenBounds.width/6f, screenBounds.height);
pos = new Vector2 (-screenBounds.width / 2f, -screenBounds.height / 2f);
barSize = new Vector2 (size.x / 1.5f, size.y / 40f);
barPos = new Vector2 (screenBounds.width / 36f, barSize.y*2f);
}
// Update is called once per frame
void Update () {
size = new Vector2 (screenBounds.width/6f, screenBounds.height);
pos = new Vector2 (-screenBounds.width / 2f, -screenBounds.height / 2f);
barSize = new Vector2 (size.x / 1.5f, size.y / 40f);
barPos = new Vector2 (screenBounds.width / 36f, barSize.y*2f);
GameObject pl = GameObject.Find ("Player");
if ( pl != null)
{
HealthScript hlth = GameObject.Find ("Player").GetComponent<HealthScript>();
if ( hlth != null)
{
percentage = (GameObject.Find ("Player").GetComponent<HealthScript> ().health)/(GameObject.Find ("Player").GetComponent<HealthScript> ().maxHealth);
}
}
}
void OnGUI() {
//draw the background:
GUI.Box(new Rect(pos.x, pos.y, size.x, size.y), bgTex);
//draw the health bar:
GUI.Box(new Rect(barPos.x, barPos.y, barSize.x, barSize.y), emptyTex);
GUI.Box(new Rect(barPos.x, barPos.y, barSize.x*percentage, barSize.y), fullTex);
}
}
Answer by tanoshimi · Aug 08, 2014 at 06:35 PM
Looking at your calculations of size
and pos
, you're placing your texture off the left hand side of the viewable screen space. (Let's say that "q" is screenBounds.width / 2. Then the top left of your box is -q, but it's width is q/3, so it never reaches the left hand side of the screen)
But (0,0) is the middle of the screen, not the top-left corner, right? The HUD object is positioned right where the camera is, so I suppose that -width/2 should be exactly the left hand side of the screen. Am I wrong?
Answer by felixpk · Aug 08, 2014 at 06:47 PM
I just can guess, but I think the textures are out of your screen. For test purpose set your Textures in the middle of your screen and look if they are visible.
Like:
void OnGUI(){
GUI.Box(new Rect(Screen.width / 2, Screen.height / 2, 200, 200), emptyTex);
}
If you can see your Image now: you know where your Problem is.
Furthermore remove all that stuff like size, pos, barSize out of your update, that's simply useless, unless the pos and size would change during runtime, but even then you must not Initialize the variable on each Update! Do it like so:
void Start() {
pos = new Vector2(screen.width / 2, screen.height / 2);
}
void Update() {
pos.x = Screen.width / 2;
pos.y = Screen.height / 2;
}
Just give it a try and tell me what's happening.
Thanks for help, but the problem was simply that I thought (0,0) would be the position of the object the script's attached to. It looks like GUI coordinates work in a different manner and I'll have to learn more about them. PS I updated all the stuff because I want the game to still work after resizing the window in runtime. Thanks for help :)
Your answer
Follow this Question
Related Questions
Sprite not showing in spriteRenderer 2 Answers
Health Bar/Hearts? 2 Answers
Help making basic health bar script 1 Answer
Health Bar / GUI issues (WorldToScreenPoint) 1 Answer
RTS health bars 2 Answers