Search Unity

"Simulate" user click

Discussion in 'Scripting' started by Santako, Feb 14, 2021.

  1. Santako

    Santako

    Joined:
    Apr 13, 2018
    Posts:
    17
    Hi there!

    Is it possible for a script to simulate that the user have clicked? I mean, if an user clicks on the screen it triggers "Input.GetMouseButtonDown(0)" but I need to "re-trigger" this at certain point without another user input. Is that possible?

    Thank you,
     
  2. Tarodev

    Tarodev

    Joined:
    Jul 30, 2015
    Posts:
    190
    Could you put whatever logic runs after the input check into a function, then just call that function later on? For example:

    Code (CSharp):
    1.  public void Update() {
    2.         if (Input.GetMouseButtonDown(0)) MyClickLogic();
    3.     }
    4.  
    5.     private void MyClickLogic() {
    6.         // Do cool stuff
    7.     }
     
    Santako likes this.
  3. Santako

    Santako

    Joined:
    Apr 13, 2018
    Posts:
    17
    Thanks for answering. At first I was looking for any kind of method to "fake" that the user has clicked but I've seen that fake inputs is quite hard on Unity. So, I've implement your solution by just moving a couple of things in the script.