• 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 Inblinder · Feb 06, 2018 at 08:51 PM · c#raycastgetmousebuttondown

Script Not Working Raycast GetMouseButtonDown()

After I start the game and click on 2D gameObject with tag "Star".. only Debug.Log("OK"); executes. Script is not attached to the gameObject with tag "Star". Help please.

 if (Input.GetMouseButtonDown(0))
             {
                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                 RaycastHit hit;
                 
                 Debug.Log("OK");
                 
                 if (Physics.Raycast(ray, out hit))
                 {
                     if (hit.transform.tag == "Star")
                     {
                         Destroy(gameObject);
                         Debug.Log("GREAT");
                     }
                     
                     Debug.Log("HMM");
                 }
             }



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
1
Best Answer

Answer by yummy81 · Feb 06, 2018 at 10:14 PM

That's because you are destroying yourself. But what you want to do is to destroy the object you clicked on, so replace this line of code:

 Destroy(gameObject);
 

with this:

 Destroy(hit.transform.gameObject);
 
 

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 Inblinder · Feb 07, 2018 at 08:43 AM 0
Share

Thx for that... but as i said only Debug.Log("O$$anonymous$$") executes ... do you know why ? (Does it matter, that it is 2D gameObject?)

avatar image yummy81 Inblinder · Feb 07, 2018 at 09:08 AM 1
Share

The reason why only Debug.Log("O$$anonymous$$") executes is as I said in my answer. In your code, you are destroying yourself. Let's take a closer look at the sequence of Debug.Log execution. When you click with the left mouse button - not at the collider but just at some point in space - you will always receive Debug.Log("O$$anonymous$$"). But when this click is received by collider, the first command your code executes is Debug.Log("O$$anonymous$$") and then Destroy(gameObject). That immediately destroys the object where your script (your code) is placed and therefore later commands - Debug.Log("GREAT") and Debug.Log("H$$anonymous$$$$anonymous$$") - are not executed. Because - once again - you destroyed yourself. You destroyed the gamebject with your code. This way, there is no gameobject with your script at the scene, so everything stops working. Try experiment a bit with this and for example put

 Debug.Log("GREAT");
 

before:

 Destroy(gameObject);
 
 

Now, when you click the collider you will see two messages in the console: "O$$anonymous$$" and "GREAT". You won't see "H$$anonymous$$$$anonymous$$", because you destroyed your gameobject. And because your gameobject is no longer at the scene, nothing will happen when clicking at anything.

avatar image Inblinder yummy81 · Feb 07, 2018 at 10:18 AM 0
Share

https://answers.unity.com/answers/1465388/view.html

avatar image
1

Answer by Inblinder · Feb 07, 2018 at 10:17 AM

Im sorry but if i'll do that only with this:

  if (Input.GetMouseButtonDown(0))
                  {
                      Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                      RaycastHit hit;
                      
                      Debug.Log("OK");
                      
                      if (Physics.Raycast(ray, out hit))
                      {
                          if (hit.transform.tag == "Star")
                          {
                              //without Destroy
                              Debug.Log("GREAT");
                          }
                          
                          Debug.Log("HMM");
                      }
                  }

It still executed only ''OK''. Have i mentioned that it is collider 2d? - does it matter? I have read somewhere that the raycasts work only for 3d objects. Try it in your unity 2d- 1) Create a sprite with Circle collider add rigidbody2d component 2) give it tag "Star" 3) copy the code 4) see what happens

Comment
Add comment · Show 4 · 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 Inblinder · Feb 07, 2018 at 10:19 AM 0
Share

And... attach the script to some gameObject in game... but not to the Sprite with tag "Star".

avatar image yummy81 Inblinder · Feb 07, 2018 at 11:08 AM 1
Share

I rewrote the entire code to suit Physics2D demands. That's what I've done step by step. I selected the $$anonymous$$ain Camera, changed its positon to: x: 0, y: 0, z: -1 and changed projection to orthographic. Next, I created empty GameObject and dropped the script ("Ray2DTest") on it. Script is below. And finally I created four 2D Objects - Sprites. I positioned each of them as follows: x: random, y: random, z: 0. And I added CircleCollider2D to each of them. It's optional whether to add Rigidbody2D or not. I tagged each sprite "Star". Here's the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Ray2DTest : $$anonymous$$onoBehaviour
 {
     private void Update()
     {
         if (Input.Get$$anonymous$$ouseButtonDown(0))
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             Debug.Log("O$$anonymous$$");
             
             RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
 
             if (hit.transform==null) return;
             
             if (hit.transform.tag == "Star")
             {
                 //Destroy
                 Debug.Log("GREAT");
             }
 
             Debug.Log("H$$anonymous$$$$anonymous$$");
         }
     }
 }


avatar image Inblinder yummy81 · Feb 07, 2018 at 01:29 PM 0
Share

One last question: Is it better to do that with Raycasts or On$$anonymous$$ouseDown() ?? I still have a problem - the objects are moving and some detect my click and some after several times clicking on them and some won't even detect my click. Is it because of my Pc? (Fps, $$anonymous$$ouse,(i've tryied that with touchpad as well)) The script above works fine but my detection of click is weird.

Show more comments

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

447 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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

Distribute terrain in zones 3 Answers

Reload clips on Gun-script not applied properly 1 Answer

Clicking on an Object to Make it the Variable Target 1 Answer

Player Attack Script. I Need Help! 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