Simple touch counting program not working
Hi. I'm totally new to Unity and Android development, and I'm trying to make a simple program that counts the touches on an Android mobile device. So what I am trying to do is count every time the user "taps" the screen, meaning that the touch is counted only once when the user touches the screen then lifts his finger off the screen. And if the user holds their touch for more than 3 seconds, I want to count the number of touches for every frame up until the user lifts his finger.
I have also made a reset function that is linked to a button, so that I can reset the number of counts to 0 when I click the button.
The problem is that when I run the app on my phone, when I touch the first time, the counter goes up by 1, then it does not count for 3 seconds no matter what I do (even though touches are being registered in the phone). After the 3 seconds it begins to count touches every frame, and not once per touch.
What I have figured out is that line 28,
if (touch.phase == TouchPhase.Ended) { touchStarted = false; numTouch = 1000000000; }, is never reached for some reason. BUT if I click the reset button, which calls the resetCounts() method, the if (touch.phase == TouchPhase.Ended) is reached and numTouch becomes 100000000. After the reset button is clicked, the app does not count for another 3 seconds, and then seems to count touches every frame afterwards.
I am completely lost in why this program is behaving so strangely, and would like to get help. I thank you in advance and will give more information that is needed.
+I also wonder how other people are debugging their Android apps. I tried Unity Remote5 but it doesn't work, so currently I build and install the app on my mobile device every time I want to run my program. This is also why I set numCount to 100000000 in the code; so that I can sort of debug my program on my phone.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class TouchCounter: MonoBehaviour
{
public Text touchCountTextBox;
public static int numTouch = 0;
private static bool touchStarted = false;
private static float touchStartTime = 0.0f;
private static float touchDelay = 3.0f; //time the user has to hold down touch for fast count to start(counts touch every frame)
private static Touch touch;
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0)
{
if (!touchStarted)
{
touchStarted = true;
touch = Input.GetTouch(0);
touchStartTime = Time.time;
numTouch++;
}
if (touch.phase == TouchPhase.Ended)
{
touchStarted = false;
numTouch = 1000000000;
}
//if user holds down touch, count touch every frame
else if (Time.time - touchStartTime >= touchDelay)
{
countTouchFast();
}
}
touchCountTextBox.text = "" + numTouch;
}
//counts number of fingers on screen every second
void countTouchFast()
{
numTouch += Input.touchCount;
}
public void resetCounts()
{
touch = Input.GetTouch(0);
while (touch.phase != TouchPhase.Ended)
{
numTouch = 0;
}
Debug.Log("reseted");
}
}
Your answer
Follow this Question
Related Questions
Touch screen not working on Android build? 0 Answers
All object move together when try to drag one object only 0 Answers
Multi-touch game can only shoot 1 ball at a time 0 Answers
How to pick up an object when touching a button(Mobile) 0 Answers
Hello I have a problem with touches in Unity 2020.1 0 Answers