- Home /
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");
}
}
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);
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?)
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.
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
And... attach the script to some gameObject in game... but not to the Sprite with tag "Star".
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$$");
}
}
}
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.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Raycast2D is inconsistent 0 Answers
2 raycasts on the fpwalker/camera 1 Answer
How do I make the object that I touched move to a location 1 Answer