GetKey in OnMouseDown Not working

When I run this and of course the object has a collider, I click the object and it will print clicked.
If I hold down the A key or any key for that matter it doesnt respond.

Can anyone test this code and tell me if it is just my system. Because I had something similar working before.

Thanks

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class buttonstuff : MonoBehaviour {

    // Use this for initialization
    void Start () {
       
    }
    void OnMouseDown()
    {
        print("clicked");
        if(Input.GetKey(KeyCode.A))
        {
            print("clicked A");
        }

    }


    // Update is called once per frame
    void Update () {
       
    }
}

If you are holding down the A key while you click, you should see both messages.

If, on the other hand, your goal is to click and then push the A key to see the second message, this will not work. You will need a more robust method that tracks the state of input over several frames.

Ok, Thank you.

Yes, I am trying to hold the A key and click the object though it doesnt work.
It does not respond to clicking when the A key is pressed.
Are you able to run the snippet?

I tested this and saw both messages while holding A and clicking my test object.

public class test : MonoBehaviour
{
    void OnMouseDown()
    {
        Debug.Log("click");
        if (Input.GetKey(KeyCode.A))
        {
            Debug.Log("A");
        }
    }
}

Awesome. So do you have any recommendation for what could be causing my problem?
Is something stuck somewhere I should reset?

I’d want to verify that you see “clicked” when clicking on the object without holding the A key. That’s step 1. If you see a single message when the A key is depressed, that tells us that the OnMouseDown() method is executing properly. Then, if you continue to see only one message while holding the A key and pressing, that becomes a very strange thing.

Correct.
When I click on the object with the mouse I see “clicked” BUT if I hold down ANY key it doesnt respond at all.

I’m stumped. You may have found a Unity bug.

Unity recommends that you call Input.Getkey in Update( and only Update() ) as thats when its flags are reset. it may be that your OnMouseDown is firing before Unity populates the input for the current frame but after the flag was reset.

Sure you can call Input.GetKey outside of Update, and it’ll usually work, but its not guaranteed to behave consistently as intended.

Thanks kru I appreciate the help.

Joshua, I also did what you recommended, where in OnMouseDown I enable a boolean like “isclicked”
then in Update() I check if Input.GetKey(KeyCode.A) && isclicked. I experience the same problem.
Its as if holding down a key is preventing OnMouseDown from working.

Thanks

Then something else is affecting the input, or the object in your scene. I’ve tried the code posted myself as well and it works fine. Try the code in an empty scene and see if theres still an issue.

also take in mind that if you called Input.ResetInputAxes() in the same frame, or some other invisible asset obscures the object, or disabling the gameobject/script can also prevent the the logs from firing.

did you post the exact code that you are using? or a simplification?

I am running only that code.

I had it run as part of a larger project and then it suddenly stopped working, though I wasnt sure if my other code was the problem, then I isolated it to it not working on its own - new project and only that code. It still doesnt work which is why I wasn’t sure if there is something wrong with my machine or I don’t know what.

I do remember it worked until the recent Windows 10 Update. So I thought maybe it changed some files on my machine so I uninstalled and installed unity and created a fresh project and tried the code - same result.
Could it be that windows 10 update is the issue?
Are you running Windows 10 etc?

Check your A key is working. Does notepad respond to it?

Then do a GetKey(“A”) in Update. To confirm the key press gets to Unity at all.

I’ve previously spent hours tracking down input bugs only to realise it was a hardware fault. I think your A key might be broken.

Yes A works fine.

Its actually any key.
Any key I hold down prevents OnMouseClick from working

Ok. I figured out the cause of the problem.

Thanks for everybody’s help.

Long story short, I decided to connect an external mouse and try again ( I am using a laptop), lo and behold it works!

So I try and hold a key down and click (with the trackpad) a windows icon and it doesnt work, use the external mouse and it works fine.

So it might be a hardware problem?

Anyway problem solved.
Thanks