- Home /
touchcount does not reset between game runs
I am making a simple game where I just want to detect when the user touches the screen. When I first boot up the project, it works as expected, but every subsequent run, Input.touchCount starts off at the last value from the previous run (usually 1), without me touching the screen at all. So I need to completely restart Unity every time I want to test something. Does anyone know why this is?
My code:
void Update()
{
Debug.Log("Touches: " + Input.touchCount);
if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
{
//Do Something...
I am using Unity 2021.1.20f1 on a Lenovo Yoga that has a touch screen and Input.touchSupported being true
Does the "Touches: " printout never go down to 0 again in the first run, when you release your fingers?
So basically the user touches anywhere on the screen to lock a rotating object into place, if the user locks the object within a certain distance of a target, the process continues, but if the object is too far from the target, the game ends. In the first situation (that of a "successful" touch) it goes back down to zero and the game continues as normal. But in the event of an unsuccessful touch, and the game ends, upon the next run, touchCount, and therefore the printout, starts off at one without any user input. I can post the full code if needed.
Edit: The game is able to work if I change Input.touches[0] to Input.touches[Input.touchCount - 1] in the if statement, otherwise the game does not do anything when the screen is touched (the object is not locked and keeps rotating as if the screen was never touched). However, this seems to not be a good solution since Input.touches is storing Touches from previous runs which are not longer relevant in a current run.
Your answer
Follow this Question
Related Questions
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
How to distinguish between two touch screens for input in android 0 Answers
How do you get position from fingerId 1 Answer
How can we make touch controls like Hole.io/ Bumper.io? 0 Answers
When pressing a button, it counts as pressing the screen and the button [New Input System] 0 Answers