OnTriggerEnter not working

Hello everyone,

I am trying to have the player be able to walk through a wall (game object) that is set inactive on Awake and then once they walk through it (activating the tigger), I want it to set the Box Collider (attached to the game object) active so they cannot go back through the same wall they just went through.

Here is the script I have

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SetWallInactive : MonoBehaviour
 
 {
     [SerializeField] BoxCollider col_wall;
     void Awake()
     {
         col_wall.enabled = false;
     }
 
     void OnTriggerEnter(Collider col)
     {
         if (col.tag == "Player")
         {
             col_wall.enabled = true;
         }
     }
 }

I was going to try “Destroy(other.gameObject);” to make it so the player wouldn’t be able to go back through as the wall has the box collider on it and is set as “is trigger”. But that is a side note, as it is not even setting the Box Collider active after the player goes through the trigger.

At Awake the level will set the wall inactive as it should but when the player walks through, it does nothing.

Any help is very welcomed, this seemed as an easy thing to do, and it has been a nightmare to figure out.

  1. Make the wall’s collider default to isTrigger being true. The in OnTriggerExit, set the collider isTrigger to false so it’s a normal collider. By having it in enter, the wall would turn solid with the player still inside it.
  2. You may also want to have it record the player’s position in OnTriggerEnter and compare it with their position on exit to make sure they’re exiting from the other side. Otherwise players might walk partly in, walk backwards and then be stuck behind a solid wall.
  3. At least one of the objects needs a rigidbody and it can NOT be set to kinematic.
  4. On the Physics settings layer matrix, the box where their layers intersect MUST be checked.
  5. If you’re using trigger settings like I’m suggesting, make sure the wall collider and object are both enabled.

So if I understand this correctly. The wall GameObject has a trigger collider and this script attached to it. and this wall GameObject is active.

And this GameObject has an active child that has a non-trigger collider on it and is referenced in the script as col_wall. On Awake this col_wall is disabled by this script.

The logic seems sound. It may be a configuration issue.

Is col_wall definitely set to be this child box collider and not the trigger collider. Do you noticed the trigger collider become disabled?

Does the player have a rigidbody?

Are they on the same physics layer? Or on layers that are set to interact with each other?

Might want to change your title for this question as you are using OnTriggerEnter in your code not OnCollisionEnter


Try looking at this example project:

https://we.tl/JXTDI5uGX4