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

Put a delay between mouse clicks.

Discussion in 'Scripting' started by elyes51, Jul 23, 2021.

  1. elyes51

    elyes51

    Joined:
    Feb 24, 2021
    Posts:
    62
    Hello, I am on a little problem that is blocking me. I want to make at the end of the game on the game over screen that the player has time to see the score
    by putting a delay of 3 seconds before the next click on the screen by making the mouse click or the screen inactive for a while (3s)


    Thanks in advance !

    Here is my script but it does not work :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class delay : MonoBehaviour {
    6.  
    7. var TimeT:float = 0;
    8.  
    9. function Update ()
    10. {
    11. TimeT += Time.deltaTime;
    12.  
    13. if (Input.GetMouseButtonDown(0)))&&TimeT > 3)
    14. {
    15.     //Other Code
    16.     TimeT = 0;
    17. }
    18. }
     
    abdullahkhan_xy likes this.
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    This is a JS variable declaration, and js has been deprecated for a long time.
    If you are following a tutorial, find a new one instead.

    This doesn't help us. Why doesn't it work? What happens instead?
     
    abdullahkhan_xy and Joe-Censored like this.
  3. elyes51

    elyes51

    Joined:
    Feb 24, 2021
    Posts:
    62

    Thank you for your reply.
    Indeed I followed a tutorial. It doesn't work means it doesn't compile, it has errors.

    Assets\delay.cs(7,10): error CS1003: Syntax error, ',' expected
    Assets\delay.cs(7,11): error CS1002: ; expected
    Assets\delay.cs(7,17): error CS1519: Invalid token '=' in class, struct, or interface member declaration
    Assets\delay.cs(13,33): error CS1525: Invalid expression term ')'
    Assets\delay.cs(13,34): error CS1525: Invalid expression term '&&'
    Assets\delay.cs(18,2): error CS1513: } expected
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    ye, abandon the tutorial, it's outdated
     
  5. elyes51

    elyes51

    Joined:
    Feb 24, 2021
    Posts:
    62
    This is what I'm gonna do. But how do you make a delay between clicks?
     
  6. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    The code you have is more or less fine once you C#ify it.

    var TimeT:float = 0; Needs to turn into float TimeT = 0;

    if (Input.GetMouseButtonDown(0)))&&TimeT > 3)
    has ))) where really only one ) should be

    function Update() needs to turn into void Update()

    And at the very end you're missing a }
     
    Joe-Censored likes this.
  7. elyes51

    elyes51

    Joined:
    Feb 24, 2021
    Posts:
    62

    Now it compiles without error. But I can click without delay. Thanks anyway !
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    This is also the 2D forum and has nothing to do with mouse clicks. I would suggest the scripting forum for general scripting queries like this.
     
  9. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    I'm certain this was and is in the scripting forum
     
    MelvMay likes this.
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    Looks like this is another case of me being an admin and moving posts and not noticing it's moved me to the target forum; too much forum hoping. Damn my terrible admin skills! Sorry. :)
     
    gorbit99 likes this.
  11. elyes51

    elyes51

    Joined:
    Feb 24, 2021
    Posts:
    62
    Thanks, I'll go take a look.
     
  12. elyes51

    elyes51

    Joined:
    Feb 24, 2021
    Posts:
    62
    It does not matter ! You have already helped me to move forward a bit.
     
  13. KAW0

    KAW0

    Joined:
    Jan 1, 2017
    Posts:
    15
    Maybe try UniTask
    https://github.com/Cysharp/UniTask
    Code (CSharp):
    1.  
    2. async UniTaskVoid Start()
    3. {
    4.         CancellationTokenSource cts = new CancellationTokenSource();
    5.         cts.RegisterRaiseCancelOnDestroy(this);
    6.         await UniTask.WaitUntil(() => Input.GetMouseButtonDown(0),
    7.         cancellationToken: cts.Token);
    8.         ShowScorePanel();
    9.         await UniTask.Delay(3000, cancellationToken: cts.Token);
    10.         await UniTask.WaitUntil(() => Input.GetMouseButtonDown(0),
    11.         cancellationToken: cts.Token);
    12.         ClosePanel();
    13. }
    14.  
     
    Last edited: Jul 23, 2021
  14. elyes51

    elyes51

    Joined:
    Feb 24, 2021
    Posts:
    62

    Thanks for your help.
     
  15. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,302
    Don't use code that you don't understand. It is overly complicated for what you are trying to do anyway

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ClickCooldown : MonoBehaviour
    6. {
    7.     //Default to 3 second cooldown
    8.     public float Cooldown = 3f;
    9.  
    10.     //Used as a count down timer
    11.     public float CooldownCountdown = 0f;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.    
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         //if the timer has elapsed
    23.         if (CooldownCountdown < 0f)
    24.         {
    25.             //check if the mouse is being clicked
    26.             if (Input.GetMouseButton(0))
    27.             {
    28.                 //reset the cooldown timer
    29.                 CooldownCountdown = Cooldown;
    30.                 //print a message to the console
    31.                 Debug.Log("Registered click");
    32.             }
    33.         }
    34.         else
    35.         {
    36.             //Countdown the timer with the time past in the last frame
    37.             CooldownCountdown -= Time.deltaTime;
    38.         }
    39.     }
    40. }
    41.  
    One thing to note about this code, is that it's not very accurate in timing. You are going to be overshooting your cooldown
    if (CooldownCountdown < 0f)

    making it if (CooldownCountdown <= 0f) isn't really going to help either, as the chances of you landing on a frame where the float is exactly 0 is very small. and float comparison is not very accurate because floats are not very accurate.

    This means that people with higher fps will have slightly lower cooldowns in practice, so just keep that in mind. It really depends what you are doing if this really matters.

    CooldownCountdown will hold the time to how much you overshot. But if you really cared about accuracy that much, you also wouldn't use floats.
     
    Last edited: Jul 25, 2021
  16. elyes51

    elyes51

    Joined:
    Feb 24, 2021
    Posts:
    62

    Thank you for your help and your explanations, I understand better now.