- Home /
Input.GetMouseButtonDown(0) not working
I have this script :
if(Input.GetMouseButtonDown(0)) {
Debug.Log("Clicked");
//some more stuff
}
For some reason, the click just doesn't work. I put the debug.log thing on purpose because I want to see if it actually detects the click. No it didn't. No errors or anything. when I clicked it the debug log showed nothing. I don't get why it doesn't work. Does anyone have any ideas? Thank You very much
I've got in a function start..... because I wan't it only to be once called at the start....
this statement doesn't make sense, the ti$$anonymous$$g to evaluate whether the mouse button is down during the invoking of the start method in your code would be incredible and worthless, put it in an update method, use a variable to constrain it if you desire. Stop kicking the box down the road and pick it up and put it where it needs to go.
As @Landern says, placing in Start is quite pointless. The function doesn't last long enough to put anything like user input into it. The function would not even last long enough to account for the reaction time of the user to know he has to click something, and if you delay the function you are delaying the running of Update().
You have 3 answers that say basically the same thing. It's a strong consensus to ignore.
If you wanna get all freaky about the method, perhaps use Start() to spawn a gameobject with the script attached and then destroy the object after your code is executed the one time. That should satisfy your insistence on using Start().
Answer by Ekta-Mehta-D · Aug 15, 2014 at 12:12 PM
If you want this to work only once then take one boolean variable.
public boolean allowClick = true;
Void Update()
{
if(allowClick)
{
if(Input.GetMouseButtonDown(0))
{
//do stuff here
allowClick = false;
}
}
}
No I also have other things that is not in the if input.getmousebuttondown(0) statement there's also other things in the function I want it to be only played once at the start. Is there an alternative for input.getmousebutton up and down but can be used in function starts?
what do you want in start() ?
In start , whatever function you will write. It will be executed only once.
I already knew that start can only be called once at the start thats why I used it. I want some kind of alternative in other words something similar to input.getmousebuttonup/down (0) but can be used in function Start
I already knew that function start could only be called once at the start, so that's why I used it. I want an alternative in other words a similar thing to input.getmousebuttondown that worlds in the start functions because I want it like you only can click once that the start of the game
You misunderstood Start() I think.
Start is not loaded once, and wait for your input. It will be executed once, at the beginning of your scene, it will be executed as soon as the object is loaded. You have just a single frame to press your button.
User input HAS to be in Update because it is looking for it. Start() is there so that you can initialize stuff and find set your values. What you want, (doing something just once and never ever again) is what Ekta $$anonymous$$ehta D said.
Answer by Foose · Aug 15, 2014 at 11:41 AM
"You need to call this function from the Update function, since the state gets reset each frame. It will not return true until the user has released the mouse button and pressed it again. "
Do you have it in your Update or anywhere else? :)
Scoure: http://docs.unity3d.com/ScriptReference/Input.GetMouseButtonDown.html
I've got in a function start..... because I wan't it only to be once called at the start....
But you HAVE to. It isnt working if you dont have it in the Update function.
Is there an alternative that I can use in any functions?
Foose is correct. It has to be called in the Update function like so
function Update ()
{
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
Debug.Log('Clicked');
}
}
The Update method is called every frame. So by having the check within the Update () method, you are checking every frame to see if the Left $$anonymous$$ouse Button has been pressed.
Why exactly do you only want it be called once at the start?
Why did I put it input in the start function? Because I want it to be like you only can press the button at the start of the scene @nostalgicbear
Answer by drudiverse · Aug 15, 2014 at 11:42 AM
put it in update function on code of a gameobject that is enabled.
I've got in a function start..... because I wan't it only to be once called at the start....
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How can I change this script to use GUITexture instead of GUI number, for health display? 1 Answer
Where too learn unityscript? 3 Answers
Semicolon error using JS 1 Answer
How to make a List Constructor? 1 Answer