Remove Box collider of object in unity 3d

14,066

Solution 1

Its working now with code

 Destroy(hit3.collider);

Solution 2

According to the docs on Destroy() "Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering."

Could this be the issue? If you check within the current Update function that object might not have been destroyed yet. Otherwise Destroy() should remove all the components of that GameObject.

Share:
14,066
Sona Rijesh
Author by

Sona Rijesh

Working as Web Developer. Working in PHP (codeigniter, Yii, Wordpress) Unity3d

Updated on June 27, 2022

Comments

  • Sona Rijesh
    Sona Rijesh almost 2 years

    Here is my code in Update function. The object has a box collider.

    if (Input.GetMouseButtonDown(0)) { 
        Ray ray = camera.ScreenPointToRay(Input.mousePosition); 
        if (Physics.Raycast (ray, out hit3, 400.0F)) {
            wName = hit3.collider.gameObject.name;
            Destroy(hit3.collider.gameObject);
        }
    }
    

    But the box collider is not getting destroyed.

    How can I destroy it?

  • Spider
    Spider about 6 years
    Just ran into this exact issue. Couldn't work out why my raycast was hitting an object that was "destroyed". Disabling the Collider before destroying the object fixed the issue.