Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 pedro.alonso · Dec 27, 2013 at 03:04 PM · 2d gamephysics2d

Get a mouse object click

Hi,

I have some problem getting the mouse to work, I want to click on an object to turn on/off the game. But I can't is giving me this NullReferenceException: Object reference not set to an instance of an object. I have no idea why, this is just a test. using UnityEngine; using System.Collections;

 public class OnOff : MonoBehaviour {
 
     public bool on = false;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
 
         RaycastHit2D hit = Physics2D.Raycast(Input.mousePosition, Vector2.zero);
 
         if(hit != null) {
 
             if(hit.collider.gameObject.name == "On") {
 
             Debug.Log( "faucet");
             
             }
         }
 
             
 
     }
 }

I'm a beginner, any help? Thanks. Is c#.

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

1 Reply

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

Answer by robertbu · Dec 27, 2013 at 04:13 PM

I ran into this issue. I don't know if issue is a bug or if I'm missing something, but a workaround is to check for the existence of the collider as well:

  if(hit != null && hit.collider != null)

 

Note this gets rid of the null reference, but you are not detecting a click. Take a look at Input.GetMouseButtonDown(). You will put your raycast inside of a:

 if (Input.GetMouseButttonDown(0)) {
   // Existing raycast code goes here
 }
Comment
Add comment · Show 3 · 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 pedro.alonso · Dec 27, 2013 at 06:57 PM 0
Share

The null reference has disappeared, thanks, but the click is not being detected, I have it like this:

 using UnityEngine;
 using System.Collections;
 
 public class OnOff : $$anonymous$$onoBehaviour {
 
     public bool on = false;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
 
         RaycastHit2D hit = Physics2D.Raycast(Input.mousePosition, Vector2.zero);
 
         if(hit != null && hit.collider != null) {
 
             if(Input.Get$$anonymous$$ouseButtonDown(0)) {
 
         
                 Debug.Log ("faucet");
             }
         }
  
 
     }
 }

Why?

avatar image robertbu · Dec 28, 2013 at 01:35 AM 0
Share

Likely issue is that you are not doing the raycast correctly. You need something like:

 float dist = transform.position.z - Camera.main.transform.position.z;
 var pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
 pos = Camera.main.ScreenToWorldPoint(pos);
 RaycastHit hit = Physics2d.Raycast(pos, Vector2.zero);
avatar image pedro.alonso · Dec 28, 2013 at 05:42 AM 0
Share

That did it, thanks.

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

19 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

Related Questions

GameObject not detecting collision with floor 0 Answers

Change a specific Gameobjects rigidbody gravity to horizontal (left) 1 Answer

Game Object is moving towards the left when fire button is pressed. 2 Answers

sharedmaterial.fricion behaivior in actual game 0 Answers

How to stop raycast when hitting an object in Unity? 0 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