• 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
4
Question by Gilles_aerts · Jul 04, 2015 at 12:45 PM · buttonfunctionwhilefalsetrue

Run a function aslong as a button is pressed

Hi

I have a button in my canvas. I know how to make a button run a function whenever its pressed, but how do I make it run the function as long as I hold the button down.

To be more clear i'll give you an example :

I have a bool that needs to be turned on and off ( on when the button is pressed, and off if its not pressed)

e.g. :

public void W$$anonymous$$leButtonIsPressed(){ myBool = true;

}

public void W$$anonymous$$leButtonIsNotPressed(){ myBool = false;

}

long story short : I want my bool to be true if the button is held down, and false if its not.

Thanks in advance.

Comment
Add comment · Show 3
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 fafase · Jul 04, 2015 at 01:20 PM 0
Share
avatar image Gilles_aerts fafase · Sep 16, 2015 at 09:47 PM 0
Share
avatar image Dave-Carlile · Jul 04, 2015 at 01:28 PM 0
Share

3 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by sonu124 · Sep 17, 2015 at 12:14 PM

t$$anonymous$$s code will work..

using UnityEngine; using System.Collections; using UnityEngine.EventSystems;

public class ButtonPressed : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {

 // Use t$$anonymous$$s for initialization
 void Start () {
 
 }
 void Update()
 {
     if (!ispressed)
         return;
     // DO SOMETHING HERE
     Debug.Log ("Pressed");

 }
 bool ispressed = false;
 public void OnPointerDown(PointerEventData eventData)
 {
     ispressed = true;
 }
 
 public void OnPointerUp(PointerEventData eventData)
 {
     ispressed = false;
 }

}

Comment
Add comment · Show 3 · 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 Gumemura · Feb 07, 2020 at 07:00 AM 0
Share
avatar image YehiOr · Dec 01, 2021 at 05:57 AM 0
Share
avatar image sacredgeometry YehiOr · Dec 01, 2021 at 06:38 AM 0
Share
avatar image
0

Answer by Hiten2012 · Jul 04, 2015 at 01:43 PM

Its possible by many different ways but choose any suitable for your case. For UI button than you can refer t$$anonymous$$s http://answers.unity3d.com/questions/857801/46-ui-dynamic-button-event-system-pointerenter-poi.html

If UI it not mandatory in your case You can also do it with GUI button than use Repeat Button http://docs.unity3d.com/ScriptReference/GUI.RepeatButton.html

In case of touch device input http://docs.unity3d.com/ScriptReference/TouchPhase.html

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 holliebuckets · Sep 16, 2015 at 10:30 PM

@Gilles_aerts T$$anonymous$$s thread is a great resource too! ---> http://forum.unity3d.com/threads/touch-and-hold-a-button-on-new-ui.266065/

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Call a function repeatedly while a boolean is true 3 Answers

scripting is not working for words "true" & "false" 1 Answer

How to do Multiple OnTriggerEnters 2 Answers

Probobly easy fix , int dosnt change issue. 1 Answer

Help with unresponsive function / if statement 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