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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Bug Inconsistency in disabling Box Colliders through scripts

Discussion in 'Physics' started by Wulfburk, Sep 8, 2023.

  1. Wulfburk

    Wulfburk

    Joined:
    Jun 23, 2021
    Posts:
    3
    Using Unity 2022.3.5f1.

    I have a box collider on an world canvas object that I want to select by Raycast from mouse position on left click. (I am deliberately not using a UI Button on that world canvas as I want to keep the selection system consistent with my 3d selection).

    When I select it, I want to toggle off the box collider, and then toggle it on again when I select something else/unselect it. However, I have tried toggling it off by several means through script, and it is still not working, i.e I still select it even when the collider is disabled.

    This is the raycast. Passing only the WorldCanvas layer, and ignoring colliders with isTrigger = true:

    Code (CSharp):
    1. Ray ray = CameraCache.CameraComponent.ScreenPointToRay(Input.mousePosition);
    2.  
    3. if (Physics.Raycast(ray, out RaycastHit worldHit, Mathf.Infinity, _worldCanvasLayer, QueryTriggerInteraction.Ignore))
    4. {
    5.    WorldSelectable worldHeader = worldHit.transform.GetComponent<WorldSelectable>();
    6.    if (worldHeader != null)
    7.    {
    8.        Debug.LogError(worldHit.transform.gameObject,worldHit.transform.gameObject);  
    9.    }
    10. }
    And this is the collider: Its not a compound collider nor there are any rigidbodies in it. Checking in the scene, it is positioned exactly where I want it and the dimensions are correct. There are also no layer overrides.



    The first selection works, and toggles off the boxCollider component AND the gameObject. Hells, I even went all in, and made isTrigger = true, and switched the layer to IgnoreRaycast.

    After a first selection, I disable the gameObject AND boxcollider, make isTrigger = true and change layer to ignore raycast layer.



    And the raycast still hits it. And it is definitely hitting the same object, the one that I just switched layers, disabled the collider, disabled the object, even tried alongside all that, to toggle isTrigger on and off and use Ignore Triggers on the raycast. Alas..:


    The raycast still hit the SAME collider, even though it was disabled as above. The first debug log was of the selection, which toggled the collider off. The second was the raycast hitting it even though it shouldnt.

    And I have double checked the raycast layer mask and it is set up correctly. The weird thing is that by manually changing layers or disabling collider/trigger on the inspector, during runtime, sometimes it will work. Meaning, sometimes the raycast wont hit it. Other times though, usually when the LAST raycast has hit the collider, no matter what the raycast keeps hitting it, even if its in the wrong layer, and/or the collider is disabled. If I then do a left click elsewhere and have the raycast not hit the collider, then try again over the collider, it wont trigger the collision. I am losing my mind over this.. help?
     
    Last edited: Sep 8, 2023
  2. Wulfburk

    Wulfburk

    Joined:
    Jun 23, 2021
    Posts:
    3
    Ugh. As always, it is something stupid we (I) glossed over.

    Before every selection attempt I was preemptively unselecting whatever object was previously selected. This meant that before every ray that I casted when trying to select an object, the previously disabled collider was enabled, to be triggered by the ray in the same frame later on in the code.