Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to find the gameobject of hit collider (raycast)?

Discussion in 'Scripting' started by clurpslurp, Jul 26, 2023.

  1. clurpslurp

    clurpslurp

    Joined:
    Jul 3, 2023
    Posts:
    23
    I want to create something simple, where when you click an object, they will destroy themselves.
    I'm sending out a ray, and then checking if the object hit has a tag called "Nails"
    Code (CSharp):
    1. private void Update()
    2.     {
    3.         if (SwitchCam._deskMode==true)
    4.         {
    5.             RemoveSator.satorCoverInstance.transform.position = SwitchCam.objectSpawn.transform.position;
    6.             RemoveSator.satorCoverInstance.transform.rotation = Quaternion.Euler(0, 0, -90);
    7.         }
    8.         if (Input.GetMouseButtonDown(0))
    9.         {
    10.             RaycastHit hit;
    11.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    12.             if (Physics.Raycast(ray, out hit))
    13.             {
    14.                 Debug.Log("Sent out ray");
    15.                 CapsuleCollider cc = hit.collider as CapsuleCollider;
    16.                 if (cc!=null && cc.gameObject.CompareTag("Nails"))
    17.                 {
    18.                     Debug.Log("Clicked");
    19.                     Destroy(cc.gameObject);
    20.                 }
    21.             }
    22.         }
    23.     }
    Up until line 16, everything works. Using if(cc!=null) as a condition itself works,and all objects clicked who have a capsule collider are destroyed. But adding the part that verifies if the object has the tag "Nails" does not.
    The tag is assigned on my gameobjects in the inspector, and all of the objects have capsule colliders, so I'm not sure what's causing the issue.
    If the object could be destoryed with cc.gameObject, I don't know why CompareTag doesn't work when I refer to cc.gameObject.
     
  2. tomfulghum

    tomfulghum

    Joined:
    May 8, 2017
    Posts:
    69
    Can you post a screenshot of the tagged object inspector?
     
    clurpslurp likes this.
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,143
    I'd suggest debugging the value of the tag that is on cc to make sure you are getting back what you expect.
     
    clurpslurp likes this.
  4. clurpslurp

    clurpslurp

    Joined:
    Jul 3, 2023
    Posts:
    23
    upload_2023-7-26_15-28-25.png
    there are three of these, identical to eachother except for the names
     
  5. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    541
    Code (CSharp):
    1.     private void Update()
    2.     {
    3.         if (SwitchCam._deskMode==true)
    4.         {
    5.             RemoveSator.satorCoverInstance.transform.position = SwitchCam.objectSpawn.transform.position;
    6.             RemoveSator.satorCoverInstance.transform.rotation = Quaternion.Euler(0, 0, -90);
    7.         }
    8.         if (Input.GetMouseButtonDown(0))
    9.         {
    10.             RaycastHit hit;
    11.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    12.             if (Physics.Raycast(ray, out hit))
    13.             {
    14.                 Debug.Log("Sent out ray");
    15.                 if (hit.transform.CompareTag("Nails"))
    16.                 {
    17.                     Debug.Log("Clicked");
    18.                     Destroy(hit.transform.gameObject);
    19.                 }
    20.             }
    21.         }
    22.     }
    23.  
     
    clurpslurp likes this.
  6. clurpslurp

    clurpslurp

    Joined:
    Jul 3, 2023
    Posts:
    23
    Thanks, it returned false. I have no idea what happened, but I temporarily disabled the capsule collider on an object touching the nails, and it all worked. Maybe because the other object was bigger it made it hard to click the nails? Confusing, but its working
     
  7. Elhimp

    Elhimp

    Joined:
    Jan 6, 2013
    Posts:
    71