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 camfox · Jun 15, 2013 at 03:10 PM · guijava

GUI Texture

I am trying to create a health bar for an enemy which follows the enemy on the screen above its head. Everything works fine, however I have a problem with the GUI Texture component when I try to change the width via java. Here is the code:

     var HealthPercentage:float;
     HealthPercentage = Health/StartingHealth;
     
     if(transform.FindChild("HealthBar"))
     {
         var barStart:float;
         var barWidth:float;
         barStart = transform.FindChild("1x1Border").GetComponent(GUITexture).guiTexture.pixelInset.width;
         barWidth = barStart*HealthPercentage;
     }
Comment
Add comment · Show 2
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 Kwaung · Jun 17, 2013 at 10:15 AM 0
Share

I'm not sure but I think your barStart should be a original width not your current texture width. And if you call the code in loop (every frame) the bar will have a size of 0 make it disappear. I think

avatar image camfox · Jun 17, 2013 at 10:23 AM 0
Share

Thanks for the reply. What my idea was when doing this was to have two separate GUI textures. I have one for the border (1x1Border) and one for the actual health bar which has a different name. This way I can keep the border the same size and just change the bar width. Because the border is the same size as the bar, it acts as the original width of the bar. I just need to know how to access the width component of the GUI Texture.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by gheeler · Jun 17, 2013 at 10:58 AM

what you can do is draw the background, then use begingroup to limit whats viewable of the next texture

 GUI.DrawTexture(//your background
 GUI.BeginGroup(new Rect(0, 0, width to use, height))
 GUI.DrawTexture(new Rect(0,0,originakWidth,height), //your health bar
 GUI.EndGroup();
 
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 camfox · Jun 17, 2013 at 11:11 AM 0
Share

I have thought about using a GUI texture script but it causes problems because I also have another script attached to the GUI texture game object which allows the health bar to stay above the enemy's head. I couldn't use a script for GUI texture because I can't get it to follow the enemy's head. Thanks though

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Fading GUI text 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Setting Scroll View Width GUILayout 1 Answer

How to read note by pressing "E" using RayCast only? 1 Answer

Gui Skin not showing 0 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