- Home /
gameObject tag
i am trying to log in console when ray hit coal. I tagged coal in tag inspector, but nothing comes up when ray hits the coal block. here is my code.
if(Input.GetMouseButton(0)){
Debug.DrawRay(transform.position, fwd, Color.green);
if (Physics.Raycast (transform.position, fwd, hit, 1)){
if(gameObject.tag == "Coal"){
Debug.Log("Found coal!!!");
}
}
}
Comment
Best Answer
Answer by MadJohny · Dec 27, 2013 at 09:31 PM
You are searching for your own tag, not the object that was hit tag, change the line "if(gameObject.tag == "Coal"){" to "if(hit.collider.tag == "Coal"){" and that should work
Answer by baris150490 · Dec 27, 2013 at 10:53 PM
#pragma strict
function Update () {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit) && hit.collider != null)
{
Debug.Log(hit.transform.tag);
}
}
Your answer
Follow this Question
Related Questions
Raycast on touch 3 Answers
Bug/Error in script CollectPapers 0 Answers
Enemy Raycasting Damage 0 Answers
Raycast isn't working on small distances 1 Answer
Exclude tag from raycast 1 Answer