Search Unity

Getting null script from 'hit.collider.GetComponent' when triggered from a 'MeshCollider'

Discussion in 'Physics' started by BoogieD, Aug 13, 2017.

  1. BoogieD

    BoogieD

    Joined:
    Jun 8, 2016
    Posts:
    236
    I am having trouble getting a script attached to a GameObject with RaycastHit from a MeshCollider although if I do the same with a SphereCollider it works. I can get a valid GameObect from RaycastHit in all instances below.
    Any help would be much appreciated.

    Code (CSharp):
    1.  
    2. void Update () {
    3.  
    4.     if (Input.GetMouseButton(0)) {
    5.        
    6.         Ray inputRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    7.         RaycastHit hit;
    8.    
    9.         if (Physics.Raycast(inputRay, out hit)) {
    10.  
    11.             //Works if the collider is a SphereCollider but not if it is a MeshCollider.
    12.             MyScript script = hit.collider.GetComponent<MyScript>();
    13.  
    14.             //Also tried the following with the same results.
    15.             //MyScript script = hit.collider.gameObject.GetComponent<MyScript>();
    16.             //MyScript script = hit.collider.transform.GetComponent<MyScript>();
    17.         }
    18.     }
    19. }
    20.  
     
    Last edited: Aug 13, 2017
  2. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    did your mesh collider marked as Convex ?
     
  3. BoogieD

    BoogieD

    Joined:
    Jun 8, 2016
    Posts:
    236
    I am getting the RaycastHit with a valid GameObject but getting a null script when from a MeshCollider. The same results whether the MeshCollider Convex is true or false.
     
    Last edited: Aug 13, 2017
  4. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    your code is correct, I suspect that there is something missing in your meshcollider, did you set the mesh property of the meshcollider ? your script is attached to this collider and not his parent ?
     
  5. BoogieD

    BoogieD

    Joined:
    Jun 8, 2016
    Posts:
    236
    I have the same script on both GameObjects with the different Colliders and the MeshCollider has a Mesh specified in the Mesh field.
     
  6. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    could you make a simple project with this issue, because from the code and what you said it should work!!
     
  7. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    Your query is hitting a collider belonging to a GameObject that doesn't contain your script. For that code to work your script must be attached to the same GameObject that contains the MeshCollider component.

    Probably you have a GameObject with your script, then a child GameObject with the mesh collider. In this situation the RayCast hits the collider but the GetComponent query returns null because there's no MyScript component attached to that GameObject. If this is the case, then using GetComponentInParent instead of GetComponent would work as you expect.
     
    Eric-Te likes this.
  8. BoogieD

    BoogieD

    Joined:
    Jun 8, 2016
    Posts:
    236
    I just had another look and realized with fresh eyes that I also had a 'cousin' box collider that was surrounding the mesh collider. I was using a rig from another project on this test one and even though I had the script on the correct gameObjects with colliders, that damn cousin was taking all the hits and getting all the attention!
    A test sphere collider worked because it extends outside the cousin box collider and the mesh collider being the inner most tightest fit always missed out. As usual, silly me! Thanks for the help guys, I appreciate the assistance. All working now. o_O
     
    Last edited: Aug 14, 2017
    Edy likes this.