Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Raycast Works Despite Not Finding Tagged Object

Discussion in 'Scripting' started by SiMULOiD, Apr 26, 2020.

  1. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    Can someone please explain why this raycast is working, despite
    objects not being tagged correctly? i.e It appears to somehow ignore the "Object" tag and
    works on anything with a collider.
    Appreciate your suggestions.


    Code (CSharp):
    1. {
    2.  
    3.      if (Input.GetMouseButtonDown(0)){
    4.  
    5. RaycastHit hitInfo = new RaycastHit ();
    6. if (Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition),
    7. out hitInfo, Mathf.Infinity) && hitInfo.transform.tag == "Object")
    8.  
    9. GameObject.Find("FocusToggle").GetComponent<FocusButton>().toggle = false;
    10. GameObject.Find("FocusToggle").GetComponent<Image>().color =
    11. new Color32(255,255,225,100);
    12.  
    13. gameObject.transform.position =  hitInfo.transform.position;
    14.  
    15.  
    16.         }
     
    Last edited: Apr 26, 2020
  2. elliotfriesen

    elliotfriesen

    Joined:
    Mar 17, 2020
    Posts:
    71
    Change it from tag to a layer mask because ray casts do not interact with tags but instead layer masks