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

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

Discussion in 'Scripting' started by maxmnmfox, Oct 7, 2018.

  1. maxmnmfox

    maxmnmfox

    Joined:
    Aug 15, 2018
    Posts:
    4
    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 .
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,869
    In the OnTriggerEnter() for the collider, just set the collider's .enabled to false.

    Code (csharp):
    1. void OnTriggerEnter(Collider other)
    2. {
    3.      this.enabled = false;
    4. }

    [Edit: I meant .enabled, not .enable; code fixed above.]
     
    Last edited: Oct 7, 2018
  3. maxmnmfox

    maxmnmfox

    Joined:
    Aug 15, 2018
    Posts:
    4
    Hello tried it but the word enable is an Error !

    thanks
     
  4. maxmnmfox

    maxmnmfox

    Joined:
    Aug 15, 2018
    Posts:
    4
    Update : to remove the box collider this is the code

    Code (CSharp):
    1.         other.GetComponent<BoxCollider>().enabled = false;
    2.  
    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".
    Code (CSharp):
    1.  void OnTriggerEnter(Collider other)
    2.     {
    3.  
    4.         if (other.gameObject.CompareTag("Pick Up"))
    5.         {
    6.             count = count + 1;
    7.             SetCountText();
    8.             // other.gameObject.SetActive(false);
    9.          
    10.         }
    11.  
    12.         other.GetComponent<BoxCollider>().enabled = false;
    13.        // Destroy(other.gameObject.GetComponent<BoxCollider>());
    14.     }
     
  5. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,907
    Actually, AFAIK instead of
    Code (CSharp):
    1. other.GetComponent<BoxCollider>().enabled = false;
    this is enough:
    Code (CSharp):
    1. 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.
     
    maxmnmfox likes this.
  6. maxmnmfox

    maxmnmfox

    Joined:
    Aug 15, 2018
    Posts:
    4
    Actually your right that works better as well , @halley she almost wrote the right answer

    This code i think it disable every box collider the player touched
    Code (CSharp):
    1.    
    2.    other.GetComponent<BoxCollider>().enabled = false;
    3.  
    And this just works right with "tag".
    Code (CSharp):
    1. other.enabled = false;
    thanks a lot.
     
  7. nimdesignanddevelopment

    nimdesignanddevelopment

    Joined:
    Jan 7, 2019
    Posts:
    4
    this still helped me in 2020..lol[
    Code (CSharp):
    1. myGameObject.GetComponent<BoxCollider>().enabled = false;
    when making a system to shut off certain colliders when menus were opened :)
     
  8. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    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.