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 haitam1 · Jan 04, 2014 at 11:55 PM · guibuttonif-statementspowerbar

GUI RepeatButton can only be clicked once

What i need is a GUI repeat button that only can be clicked once. I use the Gui button for a Gui power bar thats indicates how much power you use.

 void OnGUI() {
         bool eindeKlik = true;
         bool maakBarkleiner;
         maakBarkleiner = false;
         if (GUI.RepeatButton( new Rect(40, 40, 100, 50), "Lift off!") == true)    
         {
             maakBarkleiner  = eindeKlik;
             eindeKlik = false;
         }
 
         else{
             maakBarkleiner  = eindeKlik;
 
         }
         if(maakBarkleiner == true)
         {
             damage = +0.15f;    
             ApplyDamage();
             print(eindeKlik);
         }
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 NikoBusiness · Jan 06, 2014 at 08:45 AM 0
Share

so you mean it can be clicked only once then it disapears or u click and nothing happens?

avatar image haitam1 · Jan 06, 2014 at 02:28 PM 0
Share

@NikoBusiness

No, here is a example of what i wanted http://www.unity3dstudent.com/2011/01/worms-style-power-bar/ but then with a gui button.

So when I hold the gui button the powerbar charges when i release the gui button the object going to fire something.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Wiki

Answer by ziptao · Jan 06, 2014 at 04:51 PM

I think you need some sort of way to tell how long the mouse button has been pressed down, and add to the power level with that. Then once the mouse button is released, you can use a method to fire based on the power gained while the button was down.

 if (Input.getMouseButton(0)) 
 {
 
    //add amount to power level every second or however often you want
 
 
     //when mouse is released
     if (Input.getMouseButtonUp(0))
     {
 
       //fire something
 
 
     }
 }


This is the way I would go about something like this. You would also have to have a way to make sure the mouse is on the button and not just anywhere on the screen, so I would test for that as well. You might have to play around with it for a little bit, but it should work in a similar way to this.

Input.getMouseButtonDown Documentation

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 haitam1 · Jan 12, 2014 at 05:39 PM

the solution is:

  //checks where the mousebutton position is
     public Vector2  mousePosFromEvent = Event.current.mousePosition;
 
     //the rect where the  mouse position must be to use the function mouse up
     public Rect eersteButton;
 
     //release bool stand for now on false because it needs to be just pressed one time
     public bool laatlos = false;
     
     
     
         void OnGUI() {
 
                 //describe the size and position of the button rect
             eersteButton = new Rect(10, 70, 100, 100);
     
             //if mouse is on the current rect(button)
             if(eersteButton.Contains(Event.current.mousePosition))    
                {
                 //if mouse up in the current rect(button)
                 if(Input.GetMouseButtonUp(0))
                 {
                     //realeas = true
                 laatlos = true;
                     print(laatlos);
                 }
             }
 
 
                 //define the button to show up and if clicked
             if(GUI.RepeatButton(new Rect(eersteButton), "button1"))
             {
     
                 //checks if the button isn't released before and if the mouse is on te current rect position/size (button)
                 if(eersteButton.Contains(Event.current.mousePosition) && laatlos == false)
                 {
     
                     damage = +0.15f; 
                     ApplyDamage();
                     
                 }
     
             }
         }

maybe there is an easier option to do this but this works fine for me (it also works on a mobile phone)

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

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

Related Questions

Making a button desplay after pressing a button? 2 Answers

Unity GUI Style with if Statements 1 Answer

Conversion method types(void to bool)[SOLVED] 1 Answer

If button clicked statement? 1 Answer

Gui Playing Audio Trouble 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