• 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 malbari68 · Oct 25, 2014 at 02:53 AM · mouseclick

why mouse click is not working?

i attached the script below to an object. the purpose of this script is to enlarge the object when the mouse enter and then if the user click the left mouse button,it's has to count(but for time being i just used debug.log to test the script). however, when i entered the mouse, the object enlarge but when i clicked, nothing happened.where did I do wrong? hope somebody can help me.Thank you

void OnMouseEnter () {

     transform.localScale = new Vector3(2.0f,2.0f,2.0f);
     if (Input.GetMouseButton (0))
     {
         Debug.Log("it's click");

     }// end if

 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by yashpal · Oct 25, 2014 at 03:44 AM

Hello @malbari68,

OnMouseEnter() is only called when mouse is entered inside the object. so called only once.

So you want to put this

   void OnMouseEnter () {
     
          transform.localScale = new Vector3(2.0f,2.0f,2.0f);
         
      
      }

     void Update () {

         if(Input.GetMouseButtonDown (0))
         {
              Debug.Log("it's click");
      
         }// end if
      }


and might be you need this.

 void OnMouseExit()
 {
              transform.localScale = new Vector3(0.5f,0.5f,0.5f);
 }

Edited:

Or you use

 void OnMouseDown() {
                  Debug.Log("it's click");
     }

instead of

 void Update () {
 
             if(Input.GetMouseButtonDown (0))
             {
                  Debug.Log("it's click");
          
             }// end if
          }
Comment
Add comment · Show 2 · 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 yashpal · Oct 28, 2014 at 06:07 AM 0
Share

@malbari68 you want to only one script to code and don't attached with every gameObject Than

(1) you can use Raycast.Raycast collide with gameobject and detect which object is that. and perform action according to that.

(2) Otherwise you can attached only one script to all gameobject. That script is having public enum /public variable to set what action perform when it clicked.

(3) make unique script and attached with only one gameObject.

avatar image KyleIchiNi · Oct 28, 2014 at 06:58 AM 0
Share

There's also On$$anonymous$$ouseOver (). It's called every frame that the mouse is hoving over an object. It's a little inefficient if you want $$anonymous$$imal per frame calls but it can detect the mouse click while hoving over the object without a specific Update() function.

Oh and it can totally be used as a yield statement so it can be used in parallel with other per frame calls so's not to push back other, possibly more important, functions

avatar image
0

Answer by Memeparable · Oct 25, 2014 at 04:31 AM

Try putting it under

 void Update (){
 
 }
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 malbari68 · Oct 27, 2014 at 06:34 AM 0
Share

Hi Yash, i have already have On$$anonymous$$ouseExit script to change to object to normal size back.i'll try mouse down first before using update. actually i would like to attach this script to a few objects. So what should i do so that i can use this script to all the objects. i mean using the script without attaching to any object.Is that possible? hope you understand my question.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

29 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 avatar image

Related Questions

How To Perform A Mouse Click On Game Object 5 Answers

Mouse controlled Laserpointer 1 Answer

OnMouseDown doesn't work 1 Answer

Clicking button locks mouse, doesn't activate button. how do I fix this? 0 Answers

Detect mouseDown & mouseUp events on handles in editor 2 Answers


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