Quick way to simulate a mouse click

I have a script that checks for mouse clicks, when it detects a mouse click I want it to send 2 more.
What would be the best and easiest way to do it?


if (Input.GetMouseButtonDown(0))
{
//simulate one click
//simulate another click
}

Basically I want the script to transform a singe mouse click into 2 or more mouse clicks all happening almost at the same time.

Why would you want to do that? Are you thinking that may generate a ‘double click’? It won’t. :slight_smile:

The thing with GetMouseButtonDown is that it only works on the frame the button is clicked. So even if you could force a million clicks in there (you can’t), then the result would still only be getting one click this frame and then zero the next.

Maybe it would be better to post details of what it is that you are trying to achieve.

1 Like

Indeed, your question here doesn’t make much sense.

GetMoustButtonDown(0) basically checks two variables - is the button down now and was it not down last frame. There’s nothing that gets “sent”, it’s something you have to check for. Re-applying that would result in no change to those variables, and therefore no change in anything else.

That is, unless you’re using some event-based input system already, in which case we’d need to know which one.

I have a clicking game that destroies objects on mouse clicks, I have a function called destroyA() which gets called whenever somebody clicks, as a power up I want to destroy 2 game objects with a single click. I need to simulate some clicks inside an IEnumerator(to trigger the function and use wait for seconds to define the power up durations) because it sounds like the simplest way. The other way would be to call the function from another script but the function destroies objects by tag and if they get called at the same time it won’t destroy 2 objects with the same tag. So I really need a way to turn a mouse click into 3 mouse clicks that call my functions 3 times almost at the same time.

The clicks result in some function getting called, right? So expose that function and call it, rather than “simulating a click” which will end up doing the same thing.

If your destruction code is inside your click handler then I’d suggest splitting it out into its own function.

1 Like

Yeah but it doesn’t matter how many times I call the destroy function as long as they are called at the same time it still only destroies the first object with tag, if I check for mouse input then call destroyA() 3 times in a row its still the same outcome as calling it once, there needs to be a short delay between them, using wait for seconds (0.0000001f) to create a delay is still a no-no because after an object with a certain tag is destroied another object with another tag is created and due to the delay it still destroies the next game object with the original tag instead of the newly created one. On clicks this is not a problem because the function gets called only after it checks the other tags and destroies everything accordingly.

When you call Destroy() things aren’t destroyed immediately, they’re put in a queue of objects that then get destroyed after the end of the current frame.

This is just a guess since you haven’t shown any code for it, but I’m guessing your destruction function doesn’t account for that and queues the same object for destruction multiple times. I tend to avoid Unity’s tag system, so I can’t suggest a workaround using that. Personally I maintain my own collections of things, which makes it very easy to avoid situations like this.

Is there any workaround to maybe have the Destroy() function to destroy things immediately?
Maybe a way to push the thing I want to get destroyed sooner up the queue?

The whole queue gets cleared between frames, so position within it isn’t relevant. There is this, but as you can see it is “strongly recommended” not to use it, and it’s unclear as to whether it’s even meant to work outside of the Editor. There are very good reasons to not destroy things mid-frame in game engines.

Life will be far easier if you just take more direct control of the contents of your scene. When you create objects put them in an appropriate collection for however you need to look them up later. When you need to find them, use that collection instead of tags. When you call Destroy() on them, remove them from that collection at the same time.

1 Like