Set active false the second box collider...

Hi guys ! H’ve created this simple script:

    void OnTriggerEnter ( Collider other)
{
	if (other.gameObject.CompareTag ("Player")) 
	{
		Touchv.Play("Touch01");
		BigDoor_script.Instance.NumDoor += 1;
		gameObject.GetComponent<BoxCollider>().enabled = false; // THIS IS THE POINT
		gameObject.GetComponent<AudioSource>().enabled = true;
	}

}

I have an object with two box collider, i want to delete or switch off the second box collider, but my script delete the first… there is a way to delete only the second box collider ? I know i can change the order of the components but i need to delete the second box collider via script, thank you guys :slight_smile:

Well you have some options:

  1. you can use GetComponents and choose the second boxCollider (hopefully it will stay the second one)
  2. You can use GetComponentsInChildren and child a new GameObject and put your second BoxCollider there then it will ( I think) be definitely the second BoxCollider to choose.
  3. In Addition to 2. you could give your new GameObject a Tag and check your GetComponentsInChildrens List for this tag. Then you’re absolutely save and can add another BoxCollider without needing to rewrite this code again.

Check out what object has a higher velocity. This way will detect which object is colliding which object.

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            Touchv.Play("Touch01");
            BigDoor_script.Instance.NumDoor += 1;

            // This will disable the BoxCollider of the fastest object. (Probably your second object)
            gameObject.GetComponent<BoxCollider>().enabled = other.GetComponent<Rigidbody>().velocity.magnitude > GetComponent<Rigidbody>().velocity.magnitude; // THIS WAS YOUR POINT
            gameObject.GetComponent<AudioSource>().enabled = true;
        }
    }

In your script, instead of using GetComponent, declare something like

public BoxCollider firstCollider;
public BoxCollider secondCollider;

This will create fields in the Inspector for your object that you can link specific BoxColliders to, and Unity will assign those colliders to the fields in your script. Since you now know exactly which specific colliders your fields are referencing, you can choose which one gets disabled.

You can read more about this subject here and here.