Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by xandy92 · Dec 04, 2016 at 01:33 AM · androiduimobiletouchscreenbutton trigger events

Unity 3D UI Button Not Working When Touched (Mobile)?

I have a button on a canvas that is supposed to activate a pull spring on a pinball board for mobile devices as it is held down. Once the button is released, the pull spring is supposed to launch the ball onto the pinball board. The button is name "pullButton" on the hierarchy. However when the button is touched nothing happens. This script is attached to pull spring game object. I have referred to the button in the inspector in Unity.

I have looked into the Unity documentation and answers, but found no such resolution. This script currently works with a keyboard input. Any help would be appreciated!

 #pragma strict
 
 var inputButtonName : String = "Pull";
 var distance : float = 50;
 var speed : float = 1;
 var ball : GameObject;
 var power : float = 2000;
 var Button :GameObject;
 
 private var ready : boolean = false;
 private var fire : boolean = false;
 private var moveCount : float = 0;
 //var buttonTouch = false;
 
 function OnCollisionEnter(_other : Collision) {
 if(_other.gameObject.tag == "Ball"){
     ready = true;
 }
 }
 
 function Update () {
     var down = Input.GetButtonDown("pullButton");
    // var touches : Touch[] = Input.touches;
   //  for (var touch : Touch in Input.touches) {
   //      if(Input.GetButtonDown("Button")){
   //      buttonTouch = true;
   //  }
 
   //  }   
     if(Input.GetButton(inputButtonName) || down){
         //As the button is held down, slowly move the piece
         if(moveCount < distance){
             transform.Translate(0,0,-speed * Time.deltaTime);
             moveCount += speed * Time.deltaTime;
             fire = true;
         }
     }
     else if(moveCount > 0){
         //Shoot the ball
         if(fire && ready){
             ball.transform.TransformDirection(Vector3.forward * 10);
             ball.GetComponent.<Rigidbody>().AddForce(0, 0, moveCount * power);
             fire = false;
             ready = false;
         }
         //Once we have reach![alt text][1]ed the starting position fire off!
         transform.Translate(0,0,20 * Time.deltaTime);
         moveCount -= 20 * Time.deltaTime;
     }
 
     //Just ensure we don't go past the end
     if(moveCount <= 0){
         fire = false;
         moveCount = 0;
     }
 
 }


[1]: /storage/temp/83331-pull.jpg

pull.jpg (69.6 kB)
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
Best Answer

Answer by wesleywh · Dec 04, 2016 at 04:07 AM

Well something that you could do is call the script via event triggers, you said this was a UI button that the player will be pressing right?

So this will require some code restructure but this is what you could do. Get the following section of code

 if(Input.GetButton(inputButtonName) || down){

and change it to

 if(buttonPressed == true || down){

Then on the "OnPointerDown()" UI button event call a function that will set buttonPressed to true. Then when "OnPointerUp()" is used set buttonPressed to false.

Obviously you will need a function or set of functions depending on how you write it to set this var to true or false.

Comment
Add comment · Show 2 · 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 xandy92 · Dec 04, 2016 at 04:28 AM 0
Share

It worked! WOW! Thank you so much lol.

avatar image wesleywh xandy92 · Dec 04, 2016 at 04:29 AM 0
Share

You're welcome! $$anonymous$$ake sure to mark the answer ;).

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

150 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

Related Questions

Unity UI - Button Slow On Some Devices? 0 Answers

Issue with loading scenes using onMouseDown for mobile 1 Answer

How can I block Physical Raycasts from going through UI Elements? 0 Answers

Multi-touch game not working 0 Answers

How do I control my car with UI buttons? 0 Answers


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