Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

back click for reset and double click back for back scene

Discussion in 'Scripting' started by Arash_dh, Jan 26, 2021.

  1. Arash_dh

    Arash_dh

    Joined:
    Jul 13, 2020
    Posts:
    4
    hi I am new
    I am in a problem
    in my case, I want to click back, button in the android reset scene and with double back, load the first scene
    Code (CSharp):
    1.     int click = 0;
    2.     void Update()
    3.     {
    4.         if(Input.GetKeyDown(KeyCode.Escape)){
    5.             click++;
    6.             StartCoroutine(ClickTime());
    7.             if (click > 1)
    8.             {
    9.                 SceneManager.LoadScene(0);
    10.                 Debug.Log("exit");
    11.             }
    12.             else{
    13.                 SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    14.             }
    15.         }
    16.        
    17.     }
    18.  
    19.  
    20.     IEnumerator ClickTime()
    21.     {
    22.         yield return new WaitForSeconds(0.5f);
    23.         click = 0;
    24.     }
    this is my code. first scene never happen
    please help me
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    You are reloading the current scene (in the "else" clause, lines 12-14) as soon as the first click happens, without waiting to see whether there will be a second click.

    If you want to do different things for single-click and double-click, then you can't actually do the first thing on the single-click, you need to wait and do it once enough time has passed that it's no longer possible for that click to count as the first part of a double-click.
     
    Arash_dh and PraetorBlue like this.
  3. Arash_dh

    Arash_dh

    Joined:
    Jul 13, 2020
    Posts:
    4
    sorry, I can't get it
    Do you mean this is not possible?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Re-read antistone's post carefully:

    In other words, you cannot act when the first click comes in. You have to say "oh I have been clicked, start a timer... did I get a second click within the time I need, or if not, then just take it away with the first click."

    This is the only way that single-click vs double-click can be determined.

    The simplest timer is a float you set to zero, then increment by Time.deltaTime each frame, and check against your limit. Obviously you'd need a boolean to indicate that timing has started from the first click.
     
    Last edited: Jan 27, 2021
    Arash_dh likes this.
  5. Arash_dh

    Arash_dh

    Joined:
    Jul 13, 2020
    Posts:
    4
    Code (CSharp):
    1. int click = 0;
    2.     void Update()
    3.     {
    4.         if(Input.GetKeyDown(KeyCode.Escape)){
    5.             click++;
    6.             StartCoroutine(ClickTime());
    7.             StartCoroutine(backWork());
    8.         }
    9.     }
    10.  
    11.     IEnumerator ClickTime()
    12.     {
    13.         yield return new WaitForSeconds(0.5f);
    14.         click = 0;
    15.     }
    16.     IEnumerator backWork()
    17.     {
    18.         yield return new WaitForSeconds(0.4f);
    19.         if (click > 1)
    20.         {
    21.             SceneManager.LoadScene(0);
    22.             Debug.Log("exit");
    23.         }
    24.         else{
    25.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    26.             Debug.Log("reset");
    27.         }
    28.     }
    I changed my code to this and working thank you guys
    if anybody sees a bug in code or knows a better code I'll glad to share it with me