- Home /
if hit.collide.tag is not working on raycast function
This runs fine, but removes any object it hits, even though the Destroy() is inside the if(hit.collider.tag). Things not tagged enemy are removed and the Debug.Log prints out the fact that the object it's just hit was NOT tagged as enemy.
How do I rewrite this script to only remove objects tagged as "enemy"?
if (Physics.Raycast(transform.position, straight * 200, out hit))
{
if(hit.collider.tag == "enemy");
{
Debug.Log (hit.collider.tag);
Destroy(hit.collider.gameObject);
}
}
This script works fine if I use hit.collider.name ins$$anonymous$$d, but I need it to work with every tagged enemy.
Answer by Linus · Aug 11, 2013 at 01:13 AM
Try hit.transform.tag
And a general tip move line 5 to outside the if(hit.....) so you can see what was hit. Helps out when forgetting to set the tag on the object.
Thanks, it works! after so many tries. And I had the Debug.Log inside the if so I would know whether the if had gone through (as I had the Destroy commented out usually)
Your answer
Follow this Question
Related Questions
How does my raycast work? 1 Answer
[Problem] Cant activate 2D objects with touch? 1 Answer
Raycast that ignores object with certain tags? 2 Answers
Raycast isn't working when with tag 2 Answers