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 chammybui27 · Dec 06, 2011 at 07:54 AM · guihealthbar

GUI healthbar

Hi there. I need help from anyone. Im new in unity i'm doing my healthbar. I'm using texture. Im not sure how am i gonna declare the texture cause i keep getting error unity texture 2d cannot convert to int. all i wanted was my healthbar to change its width as the healthcount is changing too.

here's my script. hope someone can help me guys.

 var maxHealth = 100;
 var currHealth;
 var damage :int = 10;
 var myHealthbar:Texture2D ;// green
 var healthbarWidth :int;
 var myhp;
 function Start () {
 healthbarWidth = 100;
  //var myhp=Instantiate(myHealthbar, transform.position, transform.rotation);
 }
 
 function Update()
     {
       if (Input.GetKeyDown(KeyCode.F8)) 
       ApplyDamage();
      
     }
 
 function ApplyDamage ()
     {
 
     maxHealth = maxHealth - damage ;
     healthbarWidth = maxHealth;
     healthbarWidth = myHealthbar;//maxHealth * 100;
     GUI.Box( Rect (10,10, 100,50), myHealthbar);
     print(maxHealth);
 
     if (maxHealth == 0)
     {
     die();
     }
     }
 
 
 function die () 
     {
     yield WaitForSeconds(2.0);
     Destroy(gameObject);
     }



thanks in advance:)

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 syclamoth · Dec 06, 2011 at 08:45 AM 0
Share

Ok, where do I start with this. Oh I know- editing your post so that I can actually read it.

avatar image syclamoth · Dec 06, 2011 at 08:47 AM 0
Share

You should try reading the documentation before attempting to use functions like GUI.Box. Also, it seems that you are trying to treat your texture2D as if it were an integer- which you obviously can't do. Try myHealthbar.width ins$$anonymous$$d.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by IMoriarty · Dec 06, 2011 at 08:44 AM

Haven't worked much with the GUI yet, but I'd start by looking at updating the Size of the GUI.Box based on the amount of health left as a percentage. So something like:

GUI.Box( Rect (10,10,healthbarWidth,50), myHealthbar);

Comment
Add comment · 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
0

Answer by chammybui27 · Dec 06, 2011 at 09:53 AM

@ syclamoth actually i wanted to put the value of the healthcount or lives into the GUI healthbar that's why i'm trying to put the healthbarwidth equals to the myhealthbar which is a texture. I don't really how to declare the texture width that when the healthcount is decreasing the texture GUI healthbar as well.hope you get what i meant.

@IMoriaty I'm trying to separate the script from the health system to the healthbar. but i'm still having problem with the texture..

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 syclamoth · Dec 06, 2011 at 09:58 AM 0
Share

Please, don't post comments as answers. It makes these threads terribly messy.

avatar image
0
Wiki

Answer by DryTear · Jun 06, 2013 at 07:17 PM

on line 24, do this :

 HealthBarWidth = myHealthbar.width;


and on line 25:

 Rect(x,x,myHealthbar.width,x)
Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Why the healtbar is moving ? 0 Answers

Adding a floating health bar 2 Answers

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

Setting Scroll View Width GUILayout 1 Answer

Any problems with this script? 1 Answer


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