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 TrewSx · Mar 28, 2013 at 06:50 PM · javascriptguievent

OnGUI and Event.current

Hello i am trying to reduce healt and mana while pressing 'H'. I know that for each event in GUI i need to use Event.current instead of Input.GetKeyDown since this is GUI event. Here is the code, when i am pressing H nothing is happening it should reduce hp and sp but it does not. Anyone could tell me why is that please ?

 #pragma strict
 
 
 var curHp      : float = 100.0;
 var maxHp     : float = 100.0;
 
 var curSp     : float = 50.0;
 var maxSp      : float = 50.0; 
 
 
 var hpBarTexture : Texture2D;
 var spBarTexture : Texture2D;
 
 var hpBarlength  : float;
 var percentOfHp  : float;
 
 
 var spBarLength : float;
 var percentOfSp : float;
 
 
 private var _hpSpWindowRect                :    Rect = new Rect(10,Screen.height / 2 + 150,190,70);
 private var _hpSpWindowID                :    int   = 1;
 
 
 function OnGUI () {
     var e = Event.current;
     
     if (e.type == EventType.KeyDown && e.keyCode == KeyCode.H)
     ReduceHelathAndMana();
     
     _hpSpWindowRect = GUI.Window(_hpSpWindowID, _hpSpWindowRect, HpSpWindow, "Health points  /  Spell points");
 
 }
 
 
 function ReduceHelathAndMana() {
             
     percentOfHp = curHp/maxHp;
     hpBarlength = percentOfHp*100;
 
     percentOfSp = curSp/maxSp;
     spBarLength = percentOfSp*100;
 }
 
 function HpSpWindow (_hpSpWindowID) {
     
         
     if (curHp > 0) 
     {
         GUI.Box (new Rect (20,20,150,20),hpBarTexture);
     }
     
     if (curSp > 0) 
     {
         GUI.Box (new Rect (20,40,150,20),spBarTexture);
     }    
     GUI.DragWindow();
     
 }
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
0
Best Answer

Answer by Unitraxx · Mar 28, 2013 at 07:00 PM

Your code inside ReduceHelathAndMana doesn't actually decrese the heatlth. Try this :

 function ReduceHelathAndMana() {

 curHp = curHp - 10;
 percentOfHp = curHp/maxHp;
 hpBarlength = percentOfHp*100;

 curSp = curSp - 10; 
 percentOfSp = curSp/maxSp;
 spBarLength = percentOfSp*100;
 }

(10 can be replaced by a value you want to decrease with)

Comment
Add comment · Show 2 · 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 Unitraxx · Mar 28, 2013 at 07:14 PM 0
Share

Yeah I see it's a matter of interpretation of the question. If he wants the value to decrease continously over time you do have to use the Time.deltaTime, but I thought he only wanted to decrease once every time he pressed H.

avatar image save · Mar 28, 2013 at 07:16 PM 0
Share

Yeah I realized I had looked passed the keyDown also. :)

avatar image
0

Answer by TrewSx · Mar 28, 2013 at 07:09 PM

ohh yes i need to subtract form curHp sorry didnt see that, but when i use this and in my scene i am pressing H after 3 or 4 presses the whole health bar disappears. I want to make reduce the health bar bit by bit

Comment
Add comment · Show 4 · 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 Unitraxx · Mar 28, 2013 at 07:11 PM 0
Share

Just use a lower value to decrease with? Take for instance my answer, and replace the 10's with 1's.

avatar image save · Mar 28, 2013 at 07:14 PM 0
Share

If you mean that you want it to decrease over time you should not use EventType.keyDown and you should connect Time.deltaTime.

 curHp -= 10*Time.deltaTime;
 
avatar image TrewSx · Mar 28, 2013 at 07:42 PM 0
Share

Sorry for my interpretation, English is not my primary language and i do have a problems with explaining stuff in it :). Well i do want to slowly reduce the health and mana values, but when i use your modification the health and mana bar remains in the same length and after few more presses it just disappears. This code below is working as i want it the only difference is that it is not in GUI window + it is not dragable.

 #pragma strict
 
 
 var curHp      : float = 100.0;
 var maxHp     : float = 100.0;
 
 var curSp     : float = 50.0;
 var maxSp      : float = 50.0; 
 
 
 var hpBarTexture : Texture2D;
 var spBarTexture : Texture2D;
 
 var hpBarlength  : float;
 var percentOfHp  : float;
 
 
 var spBarLength : float;
 var percentOfSp : float;
 
 
 
 
 
 function OnGUI () {
     if (curHp > 0) {
         GUI.DrawTexture (new Rect (50,120,hpBarlength,20),hpBarTexture);
     }
         
     if (curSp > 0) {
         GUI.DrawTexture (new Rect (50,140,spBarLength,20),spBarTexture);
     }
 }
 
 function Update () {
     percentOfHp = curHp/maxHp;
     hpBarlength = percentOfHp*100;
     
     percentOfSp = curSp/maxSp;
     spBarLength = percentOfSp*100;
 
     if(Input.Get$$anonymous$$eyDown("h")) {
         curHp -= 10;
     }
     
     if(Input.Get$$anonymous$$eyDown("h")) {
         curSp -= 10;
     }
 }
avatar image TrewSx · Mar 28, 2013 at 07:45 PM 0
Share

i see it now, thanks for your help guys it's working now

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

12 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

Related Questions

Scale GUI Texture From the Center. 1 Answer

[Solved]Label is not a member of GUI anymore. 1 Answer

The first object in selection grid can't be activated. Why? 1 Answer

Custom Inspector Invocation behaviour? 1 Answer

GUI opacity. 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