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
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.
You're welcome! $$anonymous$$ake sure to mark the answer ;).
Your answer
Follow this Question
Related Questions
Windows 10 Touch Screen input stops triggering onClick events 0 Answers
Touch screen not working on Android build? 0 Answers
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