Void OnMouseDown

So i have a simple code for a unit that will move it after i click on it to select it. I have void OnMouseDown to set a variable true and and i have a if statment in Void Update that only goes if that is true and the player clicks the mouse button again(anywhere on the screen). The problem is when i click the unit it selects him and plays the code in the if statment at the same time. Does anyone know ho i could fix that?

First of all, my english is not good. Secondly im new. If you don’t like my opinion, just ignore it.

    void onmousedown()
    {
        if (not selected)
        {
            selectedUnit = thisUnit;
        }
        if (selected)
        {
            thenMove(selectedUnit);
        }
    }

If you check this chart, you’ll see that input events occur before Update. That means that OnMouseDown detects a click, and then after that, but in the same frame, Update detects the same mouse-click.

You’ll either need to add some additional logic that allows Update to check whether the unit selection was changed in the current frame so that it can ignore that click, or else consolidate your input handling so that both selection and movement are handled by the same function.

Do you have any ideas how to do that?

One way of doing it would be to create a second variable and set it to true when a unit is selected. In Update, ignore all clicks while this new variable is true, but then set the variable to false as the very last thing you do at the end of the Update function.

Thats what my code does in the OnMouseDown. It does that then calls the if statement in the same click

Thats what the OnMouseDown already does. It does that and the if statement with the variable in the same click

That’s not what you said in the OP that your current variable does, and it’s hard to imagine how you’d be getting your current behavior if it were. I’m saying that you need two separate variables that are both set at the same time, but that are unset at different times, and are checked in different ways by your Update function. Read my previous reply carefully.