- Home /
Question by
missypooh · Mar 17, 2012 at 01:40 AM ·
multiplayerraycasthit
Raycast hit on door and doesn't do anything
Hi all, i am trying to detect a mouse click on the door and do something. Can anyone please help me see what wrong with my code snippet? it is not printing "I hit a door" and i don't know what is happening??
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if(Input.GetMouseButtonDown(0))
{
//print("i click on door");
if (Physics.Raycast (ray, hit, Mathf.Infinity))
{
//door is clicked
if(hit.transform == "Door1")
{
print("I hit a door");
if(MoveAround.collect == 0)
{
nochoice = true;
}
if(MoveAround.collect > 5)
{
choice = true;
}
else
choice = true;
}
}
}
Comment
Best Answer
Answer by aldonaletto · Mar 17, 2012 at 01:59 AM
This line is wrong:
if(hit.transform == "Door1")
{
You should compare the object's name (hit.transform.name) or tag (hit.transform.tag) to "Door1" - hit.transform is a Transform reference, not a string.
Answer by missypooh · Mar 17, 2012 at 02:05 AM
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if(Input.GetMouseButtonDown(0))
{
//print("i click on door");
if (Physics.Raycast (ray, hit, Mathf.Infinity))
{
//door is clicked
if(**hit.transform.tag == "Door1"**)
{
print("I hit a door");
if(MoveAround.collect == 0)
{
nochoice = true;
}
if(MoveAround.collect > 5)
{
choice = true;
}
else
choice = true;
}
} }
Your answer
![](https://koobas.hobune.stream/wayback/20220613065216im_/https://answers.unity.com/themes/thub/images/avi.jpg)