(Solved!) How to Destroy tagged items using raycast?
Thanks to everyone who helped im very grateful. The Script Works and destroys the object fine.
This is my Code EDIT : Changed it but still does not work
UPDATED SCRIPT Thanks To alucardj
PS
! I can hit the object and it shows in the debug but it still doesn't destroy it !
if (Input.GetMouseButtonDown(0)){
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast(ray, hit, 1000)){ // 1000 or Mathf.Infinity should be enough !
// what did the raycast hit ?
Debug.Log( "ray hit (name): " + hit.collider.gameObject.name);
Debug.Log( "ray hit (tag): " + hit.collider.gameObject.tag );
if(hit.collider.gameObject.tag == "prop")
{
Destroy(hit.collider.gameObject);
}
}
}
This was exactly what I needed, thankyou for putting up your code. $$anonymous$$ine was pretty close to this at the start, but i was using 2D objects. Somehow after paying attention to the missing collider call, I decided to to change my 2D objects to have normal box colliders and it worked.` public Transform Effect; public float damage = 100f;
// Update is called once per frame
void Update () {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
if(Physics.Raycast(ray, out hit, 1000))
{
hit.transform.Send$$anonymous$$essage("ApplyDamage", damage, Send$$anonymous$$essageOptions.DontRequireReceiver); //calls enemyhealth script and provides parameter
/*if(hit.transform.tag == "Fish")
{
Destroy(hit.transform.gameObject); //works without calling enemyhealth script
}
*/
Debug.Log("Object Destroyed?");
}
// Debug.Log("Ray was shot.");
}
}`
Answer by Seth-Bergman · Jan 05, 2013 at 11:10 PM
your code looks ok, but you can't access "gameObject" directly from the hit, instead use collider:
http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit.html
if (Input.GetMouseButtonDown(0)){
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast(ray, hit)){
if(hit.collider.tag == "prop")
{
Destroy(hit.collider.gameObject);
}
}
}
Note that for a Raycast to work, your object needs to have a Collider attached..
Still Doesent Work after i used your script as a reference Seth/ Still Stuck :(
Add a distance to your raycast. Also add a Debug to see what the raycast is hitting (it may be the correct object, but the tag may be incorrect) :
if (Input.Get$$anonymous$$ouseButtonDown(0)){
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast(ray, hit, 1000)){ // 1000 or $$anonymous$$athf.Infinity should be enough !
// what did the raycast hit ?
Debug.Log( "ray hit (name): " + hit.collider.gameObject.name);
Debug.Log( "ray hit (tag): " + hit.collider.gameObject.tag );
if(hit.collider.gameObject.tag == "prop")
{
Destroy(hit.collider.gameObject);
}
}
}
now when you raycast, you should get a message in the console window. Check that the tag is what you were expecting =]
I dont get a debug message in the console. What am i doing wrong?
Then as Seth stated, your objects need colliders to work. This may be a silly question, but is the code within a function?
Here is a test :
Create a new scene, put a cube in the scene. Attach this script to the cube (or the camera, or an empty gameObject, this is just a test). Hit play. Does the console now show Cube and untagged?
function Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(0)){
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast(ray, hit, 1000)){ // 1000 or $$anonymous$$athf.Infinity should be enough !
// what did the raycast hit ?
Debug.Log( "ray hit (name): " + hit.collider.gameObject.name);
Debug.Log( "ray hit (tag): " + hit.collider.gameObject.tag );
if ( hit.collider.gameObject.name == "Cube" )
{
Destroy(hit.collider.gameObject);
}
}
}
}
Your script is correct, it works for me. If you have tags and colliders set up on your gameobjects, this should definitely work. Can you provide some more info?
What exactly is the gameobject you're trying to hit?
Do you even get into the if (Get$$anonymous$$ouseButtonDown(0))
part of the code if you press the left mouse button?
Is that the only camera in your scene?
What if you try putting this code on your actual $$anonymous$$ainCamera, and use camera.ScreenPointToRay(Input.mousePosition);
from that script?
Your answer
![](https://koobas.hobune.stream/wayback/20220613090448im_/https://answers.unity.com/themes/thub/images/avi.jpg)