Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by markzareal · Aug 15, 2014 at 11:38 AM · javascriptinput.getmousebuttondown

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Landern · Aug 15, 2014 at 12:10 PM 0
Share

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.

avatar image meat5000 ♦ · Aug 15, 2014 at 02:03 PM 1
Share

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().

3 Replies

· Add your reply
  • Sort: 
avatar image
7

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;
                 }
       }
 }
Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image markzareal · Aug 15, 2014 at 12:56 PM 0
Share

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?

avatar image Ekta-Mehta-D · Aug 15, 2014 at 01:01 PM 0
Share

what do you want in start() ?

In start , whatever function you will write. It will be executed only once.

avatar image markzareal · Aug 15, 2014 at 01:56 PM 0
Share

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

avatar image markzareal · Aug 15, 2014 at 01:59 PM 0
Share

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

avatar image screenname_taken · Aug 15, 2014 at 03:20 PM 0
Share

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.

Show more comments
avatar image
3

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

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image markzareal · Aug 15, 2014 at 11:57 AM 0
Share

I've got in a function start..... because I wan't it only to be once called at the start....

avatar image Foose · Aug 15, 2014 at 12:07 PM 0
Share

But you HAVE to. It isnt working if you dont have it in the Update function.

avatar image markzareal · Aug 15, 2014 at 12:16 PM 0
Share

Is there an alternative that I can use in any functions?

avatar image nostalgicbear · Aug 15, 2014 at 02:51 PM 0
Share

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?

avatar image markzareal · Aug 15, 2014 at 10:37 PM 0
Share

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

Show more comments
avatar image
1

Answer by drudiverse · Aug 15, 2014 at 11:42 AM

put it in update function on code of a gameobject that is enabled.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image markzareal · Aug 15, 2014 at 11:57 AM 0
Share

I've got in a function start..... because I wan't it only to be once called at the start....

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges