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.
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’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.
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.
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.
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 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?