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 Nefix. · Apr 29, 2014 at 03:52 AM · raycastcollidertransformgetmousebuttondown

Why 2D colliders doesn't work?

First of all apologies if there are any mistakes on my english, isn't my first language. I'm a very begginer in unity and I'm learning the basics right now, I'm creating a game where the main gameObject should move to another gameObject position on mouse right click. This is the code I have:

     GameObject GetClickedGameObject()
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit))
             return hit.transform.gameObject;
         else
             return null;
     }
     
     void Update () {
         GameObject clickedGmObj = null;
         if(Input.GetMouseButtonDown(0)){
             clickedGmObj = GetClickedGameObject();
             if(clickedGmObj != null)
                 Debug.Log(clickedGmObj.name);
             alien.transform.position = (clickedGmObj.transform.position);
         }
     }

Everything is fine when I use 3D colliders, the object (alien) moves to the position I expected, but my game is in 2D, so I would like to use 2D colliders also in order to modify their shape, because every collider delimits one territory. The thing is when I use 2D coll. nothing happens when clicked.

Another doubt I have is that when I click somewhere where there isn't an object, the game is paused and this message appears in the console:

NullReferenceException: Object reference not set to an instance of an object

Movimiento2.Update () Can I ignore it? Thanks in advance.
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 arun.pandey89 · Apr 29, 2014 at 05:07 AM 0
Share

You need to clear the air first . Few questions .

1) While Setting up project details , you've created a 3D game or 2D game ? 2) Alien Gameobject is what ? 3D gameobject or 2D gameobject ? .

Just for your information Collider has nothing to do here ,If game object is not transfor$$anonymous$$g to that position . Transform is default property of each Game Object not collider . You have to deal with the NullReferenceException first .

avatar image Nefix. · Apr 29, 2014 at 04:42 PM 0
Share

1)The project is set in 2D mode. 2)Alien GameObject is a 2D object, sprite type.

$$anonymous$$y problems were solved by the answer below, but thanks for your interest in helping.

1 Reply

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

Answer by EvilTak · Apr 29, 2014 at 02:13 PM

First of all, your code is wrong for 2D. Your GetClickedGameObject() function has code for 3D not 2D. That's why you are getting NullReferenceException because clickeGmobj is null and you are trying to get transform of null object. To avoid this, include
alien.transform.position = (clickedGmObj.transform.position);

In your if statement before that. For 2D, your GetClickedGameObject() function should be:

 GameObject GetClickedGameObject()
     {
        Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        RaycastHit2D hit = Physics2D.Raycast(mousePos, Vector2.zero); //use Vector2.zero only when object will be directly under camera.
        if (hit.collider) 
          return hit.transform.gameObject;
        else
          return null;
     }
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 Nefix. · Apr 29, 2014 at 04:37 PM 0
Share

Problems solved. Lots of thanks evil tak, I didn't know the difference between 2D and 3D code in this case, forgive my ignorance. This is the C# script I have right now, and it works perfectly!

 GameObject GetClickedGameObject()
     {
         Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         RaycastHit2D hit = Physics2D.Raycast(mousePos, Vector2.zero); //use Vector2.zero only when object will be directly under camera.
         if (hit.collider) 
             return hit.transform.gameObject;
         else
             return null;
     }
 
     void Update () {
         GameObject clickedGmObj;
         if(Input.Get$$anonymous$$ouseButtonDown(0)){
             clickedGmObj = GetClickedGameObject();
             if(clickedGmObj != null) {
                 Debug.Log(clickedGmObj.name);
                 alien.transform.position = (clickedGmObj.transform.position);
             }
             clickedGmObj = null;
         }
     }

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

22 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

Related Questions

Raycast hit working in unity editor but not in deployed webplayer build? 0 Answers

Fixing Editor Mouse Offset Due To Local Raycast? 1 Answer

RaycastHit: What is the difference between hit.transform.tag and hit.collider.tag and which should I use when? 1 Answer

Difference between hit.collider.gameObject vs hit.transform.gameObject 2 Answers

Assigning a transform using the ray cast collider. 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