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
1
Question by linuxfreak23 · Nov 09, 2014 at 02:47 PM · c#transformposition

GUIButton only executes once

     void OnGUI() {
 
         if (GUI.Button(new Rect(Screen.width / 2, 0, Screen.width / 2, Screen.height), ""))    {
                 transform.position += transform.forward * forwardSpeed * Time.deltaTime;
         }
     }

I want it to execute the line every frame while i hold the button.
But yet it only executes once when i press it and not multipe times.

Comment
Add comment · Show 1
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 richyrich · Nov 09, 2014 at 04:26 PM 0
Share

You are using an I$$anonymous$$GUI (immediate mode graphical user interface) but expecting the button to maintain state - which it has no capacity to do. You could set a Boolean value to true when the button has been pressed, then set that Boolean to false once the user has released the mouse button

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Bunny83 · Nov 10, 2014 at 04:27 PM

Uhm, simply use a RepeatButton instead.

Note: Since the Unity GUI is not made for multitouch input you can't press multiple RepeatButtons at the same time. The GUI system only has one active control at a time. You would have to roll your own solution using the Input.touches array and handle the input yourself.

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 JazzWolf · Nov 10, 2014 at 04:44 PM 0
Share

huh, didn't know this existed. Awesome. Yeah this would be the best answer.

avatar image
0

Answer by JazzWolf · Nov 09, 2014 at 08:31 PM

@Richyrich is right. OnGui runs once a frame and buttons can only be detected on the mouse up. What you can do is test to see if the mouse is down in the area of the button, turning a certain bool to true to run what you want repeatedly, then have the release of the button set the bool false. I think it'd look like this:

 //creates a rectangle to test the click position and be used for the button
 Rect rect = new Rect(Screen.width / 2, 0, Screen.width / 2, Screen.height);
 
 //the hold boolean
 public bool hold;
 
 void OnGUI(){
 Event e = Event.current;
 
 //did mouse click within the rectangle of the button?
  if ((e.type == EventType.MouseDown) && rect.Contains (Event.current.mousePosition)) { 
        hold= true; //initiate holding code
 }
 
 if (hold){
 Debug.log("The button is held");
 }
      
 //if button is released, stop the hold code
 if(GUI.Button(rect, " ")) {
           hold = false;
      }
 }


Edit: I just tested the code. The object isn't moving so you gotta tweak your move code but i used a debug to confirm that holding the button continuously does something and releasing stops. Hope it helps!

Comment
Add comment · Show 5 · 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 robertbu · Nov 09, 2014 at 08:32 PM 0
Share

FYI @JazWolf - most frame OnGUI() is run multiple times (not that it impacts your answer).

avatar image richyrich · Nov 10, 2014 at 02:20 PM 0
Share

@JazzWolf - you have used the reserved keyword 'switch' as a variable name - that's not a ideal and may cause confusion.

avatar image JazzWolf · Nov 10, 2014 at 04:19 PM 0
Share

yeah woops that didn't work I meant to come back and edit it after testing and i forgot to change the variable name at first.

avatar image richyrich · Nov 10, 2014 at 05:13 PM 0
Share

Just one more switch (last line). I know you can do it ;)

avatar image JazzWolf · Nov 10, 2014 at 05:17 PM 0
Share

Thanks @richyrich -_- still having my morning coffee

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

moving random objects to random positions 1 Answer

Setting my position to the position of a gameobject 0 Answers

C# GUI.Button Transform.Position 1 Answer

How to make Camera position Independent of its Rotation? 1 Answer

Why does object lerp to above the object it is lerping to? 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