Issue with loading scenes using onMouseDown for mobile
When players tap my "main menu" button, they also usually end up activating my "play" button. The "main menu" button loads another scene, and another button appears in the same spot as the "main menu" button called "play". For some reason, if the player holds down their finger on the first button, it will also count as them pressing down on the next button unless they very quickly remove their finger. How can I make the players touch input count only once, so that the player doesn't misnavigate. my scene loading for the button presses is here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class playAgain : MonoBehaviour {
public string levelToLoad;
public bool alreadyTapped;
public void OnMouseDown(){
//use on mouse up to set bool?
if (alreadyTapped == false) {
Application.LoadLevel (levelToLoad);
alreadyTapped = true;
}
}
public void OnMouseUp(){
alreadyTapped = false;
}
}
Answer by matthew_gigante · May 22, 2018 at 04:30 PM
The solution I found for this issue was to use invoke to call a function which set a Boolean to true, this prevented the button from automatically being pressed if the finger was still held down from the previous scene.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class tutorialScript : MonoBehaviour {
public string levelToLoad;
public bool timerDone = false;
public void Start(){
Invoke ("Timer", 0.1f );
}
void Timer(){
timerDone = true;
}
public void OnMouseDown(){
if(timerDone == true){
Application.LoadLevel (levelToLoad);
}
}
}