To click the GameObject only once

Hello,
I would like to know if it’s possible to put a constraint that would make sure the object can only the clicked once. Currently I have a OnMouseDown() that performs the same instructions every time the object is clicked. I would like these instructions to be performed only once, the first time clicked.
Hope that there is solution for this :slight_smile:
Thank you!

public class SomeClass : MonoBehavior
{
   bool wasClicked = false;

   void OnMouseDown()
   {
      if(!wasClicked)
      {
         wasClicked = true;

         //Do the thing.
      }
   }
}
1 Like

Thank you so much! :smile::smile::smile:

Hey sorry there seems to be an issue I am facing on this code: it doesn’t take the first element of my list
6359913--707616--upload_2020-9-29_0-4-0.png
this code produces
6359913--707619--upload_2020-9-29_0-4-58.png
where it’s ‘total IDs: 0’ I expected a ‘total IDs: 1’ and where it’s ‘total IDs :1’ it was supposed to be 2 as there are total balls 2. Do you have any recommendations?

It looks like items are correctly being added to the List as expected, just that you’re getting the count of the List before adding a new element to it, so it prints that count value instead of the current count value.

If you were to add the object to the List first, then get the count of it, it should display 1 & 2 correctly:

void OnMouseDown() {
   Overlap overlap = FindObjectOfType<Overlap>();

   if(!wasClicked) {
      wasClicked = true;
     
      overlap.IDlist.Add(id);

      print("you clicked ball number: " + id);
      print("total IDs: " + overlap.IDlist.Count);
   }
}

Also, if you only need to find the Overlap script once as well, you should include it inside the if-statement, so that you’re not finding it multiple times again after the one-click-only logic has run.

1 Like

Thank you for correcting me ! :smile::smile::smile:

1 Like