Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kaka24fan · Aug 08, 2014 at 06:18 PM · noobonguihealthbarnot showingnot visible

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);
 
     }
 
 }
 

alt text

bez tytułu.gif (436.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kaka24fan · Aug 08, 2014 at 06:42 PM 0
Share

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?

avatar image tanoshimi · Aug 08, 2014 at 06:50 PM 0
Share

No, (0,0) is top left corner.

avatar image kaka24fan · Aug 08, 2014 at 07:02 PM 0
Share

Ok, thanks. I've made some progress. Thanks for help!

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kaka24fan · Aug 08, 2014 at 07:04 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges