How can i disable a specific box collider of an object when my player walk in it [Solved]

Hello

i want to know how can i disable a specific box collider when the player walk throw it

the player will try to find 7 switches to turn them on by just walking around them when the player find one a GUI text will count 1 of 7 .

my problem is the box collider still there so the player can go throw it again and it will count 2 of 7 how can i disable the collider if that object?

thank you .

In the OnTriggerEnter() for the collider, just set the collider’s .enabled to false.

void OnTriggerEnter(Collider other)
{
     this.enabled = false;
}

[Edit: I meant .enabled, not .enable; code fixed above.]

Hello tried it but the word enable is an Error !

thanks

Update : to remove the box collider this is the code

        other.GetComponent<BoxCollider>().enabled = false;

the problem i attached it to the player and when the player will find any box collider will disable it , i just wanted to disable the box collider of objects with tag “pick up”.

 void OnTriggerEnter(Collider other)
    {

        if (other.gameObject.CompareTag("Pick Up"))
        {
            count = count + 1;
            SetCountText();
            // other.gameObject.SetActive(false);
          
        }

        other.GetComponent<BoxCollider>().enabled = false;
       // Destroy(other.gameObject.GetComponent<BoxCollider>());
    }
3 Likes

Actually, AFAIK instead of

other.GetComponent<BoxCollider>().enabled = false;

this is enough:

other.enabled = false;

Since the Collider other is the collider itself. But I may be wrong and I’m not in the position to try it out momentarily.

1 Like

Actually your right that works better as well , @halley1 she almost wrote the right answer

This code i think it disable every box collider the player touched

   other.GetComponent<BoxCollider>().enabled = false;

And this just works right with “tag”.

other.enabled = false;

thanks a lot.

this still helped me in 2020…lol[myGameObject.GetComponent<BoxCollider>().enabled = false;
when making a system to shut off certain colliders when menus were opened :slight_smile:

I was searching for ways to disable a collider, and for what I needed, this was only the method that worked. Thanks!

It also works when you have no menus opened.