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 Harald921 · Apr 25, 2015 at 11:46 PM · guitexturehudhealthbar

How to make a texture cover an entire GUI box

So I have followed some tutorials on YouTube, and I've managed to get a hunger, thirst and health bar working. Everything is absolutely perfect except the textures I put in for the buttons refuse to cover the entire box, even though the images are of the exact same size.

Let me give you an example:

alt text

As you can see the textures (which are .PNG files of the correct resoluton [150:20] that are solid colors) What is causing this, and how can I make them cover absolutely all of the dark-grey area?

Thank you in advance, and sorry if I've missed something obvious and is bothering you.

example.png (25.7 kB)
Comment
Add comment · Show 9
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 Harald921 · Apr 25, 2015 at 11:27 PM 0
Share

Here is the script btw:

pragma strict

//Size of Textures

var size : Vector2 = new Vector2(150, 20);

//Health Variables var healthPos : Vector2 = new Vector2(20, 20); var healthBarDisplay : float = 1; var healthBarEmpty : Texture2D; var healthBarFull : Texture2D;

//Hunger Variables var hungerPos : Vector2 = new Vector2(20, 45); var hungerBarDisplay : float = 1; var hungerBarEmpty : Texture2D; var hungerBarFull : Texture2D;

//Thirst Variables var thirstPos : Vector2 = new Vector2(20, 70); var thirstBarDisplay : float = 1; var thirstBarEmpty : Texture2D; var thirstBarFull : Texture2D;

//Fall Rate var healthFallRate : int = 150; var hungerFallRate : int = 150; var thirstFallRate : int = 100;

private var ch$$anonymous$$otor : Character$$anonymous$$otor; private var controller : CharacterController;

var canJump : boolean = false;

var jumpTimer : float = 0.7;

function Start() { ch$$anonymous$$otor = GetComponent(Character$$anonymous$$otor); controller = GetComponent(CharacterController); }

function OnGUI() { //Health GUI GUI.BeginGroup(new Rect (healthPos.x, healthPos.y, size.x, size.y)); GUI.Box(Rect(0, 0, size.x, size.y), healthBarEmpty);

 GUI.BeginGroup(new Rect (0, 0, size.x * healthBarDisplay, size.y));
 GUI.Box(Rect(0, 0, size.x, size.y), healthBarFull);
 
 GUI.EndGroup();
 GUI.EndGroup();
 
 //Hunger GUI
 GUI.BeginGroup(new Rect (hungerPos.x, hungerPos.y, size.x, size.y));
 GUI.Box(Rect(0, 0, size.x, size.y), hungerBarEmpty);
 
 GUI.BeginGroup(new Rect (0, 0, size.x * hungerBarDisplay, size.y));
 GUI.Box(Rect(0, 0, size.x, size.y), hungerBarFull);
 
 GUI.EndGroup();
 GUI.EndGroup();
 
 //Thirst GUI
 GUI.BeginGroup(new Rect (thirstPos.x, thirstPos.y, size.x, size.y));
 GUI.Box(Rect(0, 0, size.x, size.y), thirstBarEmpty);
 
 GUI.BeginGroup(new Rect (0, 0, size.x * thirstBarDisplay, size.y));
 GUI.Box(Rect(0, 0, size.x, size.y), thirstBarFull);
 
 GUI.EndGroup();
 GUI.EndGroup();

}

function Update() { //HEALTH CONTROL SECTION if(hungerBarDisplay <= 0 && (thirstBarDisplay <= 0)) { healthBarDisplay -= Time.deltaTime / healthFallRate * 2; }

 else
 {
     if(hungerBarDisplay <= 0 || thirstBarDisplay <= 0)
     {
         healthBarDisplay -= Time.deltaTime / healthFallRate;
     }
 }
 
 if(healthBarDisplay <= 0)
 {
     CharacterDeath();
 }
 
 //HUNGER CONTROL SECTION
 if(hungerBarDisplay >= 0)
 {
     hungerBarDisplay -= Time.deltaTime / hungerFallRate;
 }
 
 if(hungerBarDisplay <= 0)
 {
     hungerBarDisplay = 0;
 }
 
 if(hungerBarDisplay >= 1)
 {
     hungerBarDisplay = 1;
 }
 
 //THIRST CONTROL SECTION
 if(thirstBarDisplay >= 0)
 {
     thirstBarDisplay -= Time.deltaTime / thirstFallRate;
 }
 
 if(thirstBarDisplay <= 0)
 {
     thirstBarDisplay = 0;
 }
 
 if(thirstBarDisplay >= 1)
 {
     thirstBarDisplay = 1;
 }

}

function CharacterDeath() { Application.LoadLevel("SI$$anonymous$$PLELEVEL"); }

avatar image Addyarb · Apr 25, 2015 at 11:52 PM 0
Share

Have you considered using Unity's uGUI ins$$anonymous$$d of legacy? I know some people prefer the scripted/legacy GUI, but honestly, this would be a breeze with uGUI.

In the mean time, I'll take a look at it and see if I can find anything obvious. Would you reformat the code? (Just highlight it all in your post and click the 101010 button again).

avatar image Addyarb · Apr 26, 2015 at 12:06 AM 0
Share

I think I've found the issue.

Adjust the "Size" variable in your inspector to 150 and 20.

I used your script in a new scene, and it works as expected (the texture fills the entire box).

Perhaps you accidentally altered the inspector value?

avatar image Harald921 · Apr 26, 2015 at 12:23 AM 0
Share

The size is set to 150 and 20 in the inspector. And the pictures I am using are 150x and 20y. No idea how to get this to work. :/

Oh, and I cannot reformat the code, if I try the "comment" button doesn't work. No kidding. >.<

Edit: Thanks for the help though, I am really appreciating it!

avatar image Addyarb · Apr 26, 2015 at 01:03 AM 0
Share

Sure thing!

Can you try making some new health bar images and making sure the pixels are 150/20? As I said, it works fine for me, so it's either something with your inspector values or your images. Since we've eli$$anonymous$$ated the inspector issue, that's the only other thing that could be happening from what I can see.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

How do you make a HUD Interface that expands with resolution (Game Border/GUI) 1 Answer

Getting Error "DrawGUITexture: Texture is Null" when using Graphics.DrawTexture in OnGUI() 2 Answers

Splitscreen w/ different GUIs on each camera 2 Answers

GuiTexture above 3D objects 1 Answer

Help!!! NullReferenceException: Object reference not set to an instance of an object UnityEngine.GUI.DrawTexture 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