• 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 winsjansen · Sep 13, 2012 at 03:29 PM · onmouseup

EventType.MouseUp && Event.button

Greetings,

Simple question, I am using this code and it give me this errors even though its an 100% copy from the documentation;

 void OnMouseUp() {
     Event e = Event.current;
     
     if (e.button == 0 && e.isMouse){
             Debug.Log("Left Click");
     }else{
             Debug.Log("NOT-Left Click");
         
     }
 }

Object reference not set to an instance of an object!

Someone know why this happens to me?

Thanks!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by sebstar · Sep 13, 2012 at 05:34 PM

Hi,

I think you have to implement it in the OnGUI() function. For each event OnGUI is called in the scripts (http://docs.unity3d.com/Documentation/ScriptReference/Event.html), otherwise the Event.current is null.

Additional, if you wanna check a mouse click, you can differ between mouse down and mouse up like this.

 void OnGUI()
 {
     if (Event.current.button == 0)
     {
         if (Event.current.type == EventType.MouseDown)
         {                
             //...
         }
     }
 }

I hope this helps you.

Greets

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 winsjansen · Sep 13, 2012 at 05:38 PM 0
Share

Thanks, for the reply but sadly it do not help me! but maybe you still can do!

The documentation says that you can use this on the "On$$anonymous$$ouseUp" to define what mouse button is clicked, and this is what I need to do! $$anonymous$$aybe you know another way to do it? The "Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0)" do not work for that, sadly!

avatar image sebstar · Sep 14, 2012 at 07:20 AM 0
Share

Hi,

I don't found your statement in documentation. In documentation is only written, that "On$$anonymous$$ouseUp" is called when the user has released the mouse button. (see http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.On$$anonymous$$ouseUp.html).

I have test it. On$$anonymous$$ouseUp is only called, if you release the left mouse button and you have pressed over the object, that has added the $$anonymous$$onobehaviour.

So if you only interested on the Left click, you can do this.

void On$$anonymous$$ouseUp() { Debug.Log("Left $$anonymous$$ouse Button released"); }

avatar image
0

Answer by sebstar · Sep 14, 2012 at 08:54 AM

Hi,

     // Left Click
     void OnMouseUp()
     {
         Debug.Log("OnMouseUp Left Click");
     }
     
     // Right Click
     void OnGUI()
     {
         if (Event.current.button == 1)
         {
            if (Event.current.type == EventType.MouseDown)
            {          
                    Camera currentCamera = Camera.main;        // take the current camera SceneView or gameView doesn't matter
                 if(currentCamera == null)
                     return;
                 
                 Vector3 mouseposition = Input.mousePosition;
                 Ray screenRay = currentCamera.ScreenPointToRay( mouseposition );
                 
                 RaycastHit rayhit;
                 if(!Physics.Raycast(screenRay, out rayhit))
                 {
                     return;
                 }
                 
                 if(rayhit.collider == collider)
                     Debug.Log("OnMouseUp Left Click");
            }
         }
     }

This works for me, but the implementation in OnGUI needs a collider, don't forget

Comment
Add comment · 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
0

Answer by vikingfabian-com · Aug 28, 2014 at 12:30 PM

 public static bool LeftMouseUp()
 {
         return Event.current != null && Event.current.type == EventType.MouseUp && Event.current.button == 0;
 }
Comment
Add comment · 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

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

11 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

Related Questions

Anyone have a double-click script more reliable than mine? 3 Answers

boolean flag problem 1 Answer

C# MOUSE EVENT TOGGLE 1 Answer

mouseup event does not work soon 0 Answers

Sound won't PLAY!!!!!!!!!!!!!!!!!!!!!!!!!!!! 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