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 Madswint · Nov 18, 2013 at 05:49 PM · javascriptguivariablessprintstamina

Why does it give me an error in this small java script for sprint?

    var curStam : float = 100.0;
     var maxStam : float = 100.0;
      
     var isSprinting : boolean = false;
      
     var stamCooldown : boolean = false;
      
      
     function OnGUI () {
       GUI.Label(ResizeGUI(Rect(430,350,50,50)), "" + curStam);
       
     
       
       if (GUI.Button(ResizeGUI(Rect(350,350,50,50)), "Run" && curStam > 0 && stamCooldown == false)) {
       isSprinting = true;
      }
             else {
                     isSprinting = false;
                    
            
             }
      
      
             if(isSprinting == true && curStam >= 0) {
            
             curStam--;
            
     }
      
             if(isSprinting == false && curStam < 100.0) {
      
             curStam++;
      
     }
      
      
             if(curStam == 0) {
            
             stamCooldown = true;
            
             }
            
             if(curStam == 1.0) {
            
             stamCooldown = false;
            
            
             }
     }
 
 
 function ResizeGUI(_rect : Rect) : Rect
 {
     var FilScreenWidth = _rect.width / 800;
     var rectWidth = FilScreenWidth * Screen.width;
     var FilScreenHeight = _rect.height / 600;
     var rectHeight = FilScreenHeight * Screen.height;
     var rectX = (_rect.x / 800) * Screen.width;
     var rectY = (_rect.y / 600) * Screen.height;
  
     return Rect(rectX,rectY,rectWidth,rectHeight);
 }

I'm trying to make a runescape style sprint bar, if you click on a button, it will activate sprinting so if you click somewhere, it sprints there, and when the curStam hits 0, you cant sprint anymore. Problem is the button, it worked with holding Shift in an update function, but I want it in a OnGUI.

Error is:

MissingMethodException: UnityEngine.GUI.Button

I tried to fix it by RepeatButton, does work tho. Any help greatly appreciated.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AeonIxion · Nov 18, 2013 at 07:31 PM

change

 if (GUI.Button(ResizeGUI(Rect(350,350,50,50)), "Run" && curStam > 0 && stamCooldown == false))


to

 if (GUI.Button(ResizeGUI(Rect(350,350,50,50)), "Run") && curStam > 0 && stamCooldown == false)


EDIT

 if (GUI.Button(ResizeGUI(Rect(350,350,50,50)), "Run")) 

returns true if you click on the button so every time you clicked on the button, isSprinting was set to true, but the next frame it was set back to false because you didn't keep clicking. With the following code, if you click on the button, isSprinting will be set to true if it was false and vice versa.

 if (GUI.Button(ResizeGUI(Rect(350,350,50,50)), "Run")){
     Debug.Log("clicked");
         isSprinting = !isSprinting;
     }
     
     
     
     if(isSprinting == true && curStam > 0) {
         Debug.Log("curstamdown" + curStam);
         curStam--;
         
     }
     else if(curStam == 0)
     {
         isSprinting = false;
     }
     
     if(isSprinting == false && curStam < 100.0) {
         Debug.Log("curstam up" + curStam);
         curStam++;
         
     }
Comment
Add comment · Show 10 · 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 Madswint · Nov 18, 2013 at 07:41 PM 0
Share

Ah thanks that fixed the error, but even with RepeatButton, it will only make the curStam change by -1, not keep going down to 0. How can I fix that?

avatar image AeonIxion · Nov 18, 2013 at 08:19 PM 0
Share

edited my answer, take a look.

avatar image Madswint · Nov 18, 2013 at 08:40 PM 0
Share

Thanks! Now it works perfectly. Would you know how to add something so it wont make it lower so fast? I tried InvokeRepeating("cur$$anonymous$$ax",1,1);

that didn't do anything. Any tips regarding that? Thanks again !

avatar image AeonIxion · Nov 18, 2013 at 08:48 PM 0
Share

change curStam-- to curStam -= 0.1. you can increase/decrease it as fast or slow as you want. You could change the rate depending on the player's level for example :)

avatar image Madswint · Nov 18, 2013 at 08:57 PM 0
Share

Oh god another problem popped up. It starts at 100.01, and if I start it, and let it go down to 0, it goes to 0.0515 something. Why does it do that? Can I change that, without going back to int variable?

Show more comments

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

19 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 avatar image avatar image avatar image

Related Questions

On Clicked, On Released GUI Button ? 2 Answers

How to access a non static variable by another static variable? 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Dynamic variable creation for unique GUI.Window generation 1 Answer

Adding stamina to existing 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