Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 coolbudy1998 · Aug 23, 2018 at 08:50 PM · scripting problemuibuttonevent triggering

UI Buttons to work continuously when pressed and held for sometime

Hi, so I have a UI Button which when pressed activates a FireNow() function, which fires a bullet. Now I want to fire continuously, so I have added OnPointerDownHandler and OnPointerUpHandler to my code. But I am not able to access these functions in my FireNow function. This is the code. How do I access them in FireNow(). The Shoot() function performs the real shooting.

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

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

 // Update is called once per frame
 public void FireNow () {

     if (isreloading)
         return;                             //if reloading then don't go ahead

     if(currentammo<=0)                      //if we have no ammo
     {
         StartCoroutine(Reload());           //reloading done here through coroutine
         return;                             //don't go ahead in this function
     }

     if (Time.time >=nextTimeToFire && ispressed)            //Input.GetButton("Fire1") and Time.time adds real time              
     {
         Shoot();                                            //function to shoot when left mouse button is clicked
         nextTimeToFire = Time.time + 1f / 11f;                          //shows which is the next time to fire
     }
 }
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
1
Best Answer

Answer by Hellium · Aug 23, 2018 at 09:57 PM

Simply call your function in Update while isPressed == true

 public void OnPointerDown(PointerEventData eventData)
 {
     ispressed = true; 
 }
 
  public void OnPointerUp(PointerEventData eventData)
  {
      ispressed = false;
  }
  
  private void Update()
  {
     if( isPressed ) FireNow();
  }
  
  // Update is called once per frame
  public void FireNow () {
      if (isreloading)
          return;                             //if reloading then don't go ahead
      if(currentammo<=0)                      //if we have no ammo
      {
          StartCoroutine(Reload());           //reloading done here through coroutine
          return;                             //don't go ahead in this function
      }
      if (Time.time >= nextTimeToFire)            //Input.GetButton("Fire1") and Time.time adds real time              
      {
          Shoot();                                            //function to shoot when left mouse button is clicked
          nextTimeToFire = Time.time + 1f / 11f;                          //shows which is the next time to fire
      }
  }
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 coolbudy1998 · Aug 24, 2018 at 12:32 PM 0
Share

Thank you for answering but now it is not firing a single time!! Where I am missing?? I made Update function public and passed its reference in my Button.

avatar image Hellium coolbudy1998 · Aug 24, 2018 at 03:18 PM 1
Share

No no, you don't have to pass the Update function to the onClick button.

This script must be attached to the button. Remove every listener to the onClick event. Update, OnPointerDown and OnPointerUp will be called automatically.

avatar image coolbudy1998 Hellium · Aug 24, 2018 at 04:15 PM 0
Share

Ahh that's where I was missing!! Thank You so much, it's working fine now. I was wondering since days and finally you saved me!

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

209 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 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 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 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 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 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 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 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 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 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

Pressing space calls the wrong function 1 Answer

UI Button event triggers don't work on mobile but work in eaditor? 0 Answers

How to get an array of all Buttons attached to a Panel? 2 Answers

Is there a way to have 1 Button update the values of variables of a script that is on multiple game objects? 1 Answer

Scripting button functionality inside class with created object's method 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