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

Check for mousedown clicks

Discussion in 'Scripting' started by hrohibil, May 5, 2021.

  1. hrohibil

    hrohibil

    Joined:
    Apr 17, 2021
    Posts:
    262
    How to test if a mouse has been clicked a specific number of times??

    I am unsure on how to set the xx

    void OnMouseDown()

    {

    if (xx > 5)

    //do something

    }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    You could just create an int variable and increment it each time OnMouseDown is called.
     
  3. hrohibil

    hrohibil

    Joined:
    Apr 17, 2021
    Posts:
    262
    Hello
    Guys

    How do I modify this to detect after 5 mouse clicks then do something?



    function OnMouseOver(){
    if(Input.GetMouseDown(0){
    // Whatever you want it to do.
    }
    }
     
  4. hrohibil

    hrohibil

    Joined:
    Apr 17, 2021
    Posts:
    262
    sorry but could you please write the code
    I want the object destroying after I clicked it 5 times.

    int afterFiveClick;
     
  5. vargata

    vargata

    Joined:
    Nov 26, 2013
    Posts:
    120
    Code (CSharp):
    1. int counter = 0;    //declare your int
    2.  
    3. void OnMouseUpAsButton() {    //this makes sure mouseclick is only counted if its released over the same object you clicked on.
    4.     if(counter == 5) {    //check if you are there
    5.         doWhatYouWant();    //call your function doing your stuff
    6.         counter = 0;    //reset your int
    7.     } else counter += 1;    //if not, add one to it
    8. }
    9.  
    10. private void doWhatYouWant() {
    11.     //do what you want here
    12. }
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    I could certainly write the code but wouldn't it be better for everyone involved if you learned to write the code yourself? If not, I'd just have to write your next piece of code 5 minutes later.
     
  7. vargata

    vargata

    Joined:
    Nov 26, 2013
    Posts:
    120
    sometimes a beginner needs the first pushes to see a general direction before they even know what to look for. sadly many of us forget how it was when we first took the keyboards
     
  8. hrohibil

    hrohibil

    Joined:
    Apr 17, 2021
    Posts:
    262
    Thank you very much guys.
    @PraetorBlue I meant no disrespect by asking for the help, I agree with you. I am moving from blender to learn unity and everything i googled just came up with what happens after you press the mouse button but not after a specific numbers as i wanted, Same goes for the most the tutorials i found on the movement jump with space bar, but still i can´t find one with a condition saying if you jumped and are already in the air, then you should not be able to jump while in air..
    @vargata I added the script to a simple object , sphere. But nothing happens, I don't even get an error in visual studio.
    Where in the initial script should it be? Maybe i have placed it wrong.
     
  9. hrohibil

    hrohibil

    Joined:
    Apr 17, 2021
    Posts:
    262
    Looks like this

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class deleteGun : MonoBehaviour
    6. {
    7.  
    8.     int counter = 0;    //declare your int
    9.  
    10.     void OnMouseUpAsButton()
    11.     {    //this makes sure mouseclick is only counted if its released over the same object you clicked on.
    12.         if (counter >= 2)
    13.         {    //check if you are there
    14.             doWhatYouWant();    //call your function doing your stuff
    15.             print("KILL");
    16.             counter = 0;    //reset your int
    17.         }
    18.         else counter += 1;    //if not, add one to it
    19.     }
    20.  
    21.     private void doWhatYouWant()
    22.     {
    23.         //do what you want here
    24.     }
    25.  
    26.  
    27.  
    28. }
     
  10. hrohibil

    hrohibil

    Joined:
    Apr 17, 2021
    Posts:
    262
    Sorry. Fixed it.

    Thanks .

    Awesome support
     
    vargata likes this.
  11. vargata

    vargata

    Joined:
    Nov 26, 2013
    Posts:
    120
    sure nothing happens, i don't know what you wanna do. i just put the line "//do what you want here". that's where you have to write your code. also, for an object to detect your mouse click, it must have a collider attached to the gameobject

    (on the sphere click add component -> physics -> box capsule or mesh collider) and set it to trigger