OnTriggerEnter is not responding

Hey Everyone!
I am here to ask about how to use OnTriggerEnter (I do but it doesn’t work). When i use it in this script, it doesn’t respond when colliding. Here is the full script below (OnTriggerEnter is on line 61), any ideas on how i could fix it?

(Edit: I just tested it in another script, it works, so that means the other stuff inside the script is stopping it)

public Transform objectToPlace;

  private Camera gameCamera;
  private GameObject gameCamera3;
  private GameObject MenuThing;
  private GameObject Pallete;
  private GameObject WalkButton;
  private GameObject PauseButton;

  public int CounterThing = 0;

void Start()
{

MenuThing = GameObject.FindWithTag("MenuButton");
WalkButton = GameObject.FindWithTag("WalkButton");
PauseButton = GameObject.FindWithTag("PauseButton");
Pallete = GameObject.FindWithTag("Pallete");
Pallete.SetActive(false);
}

void Update() {

      MenuThing.SetActive(false);
      WalkButton.SetActive(false);
      PauseButton.SetActive(false);
  Debug.Log("Log1");
   
      Debug.Log("Log2");
       gameCamera3 = GameObject.FindWithTag("CameraTag");
       gameCamera = gameCamera3.GetComponent<Camera>();
    Debug.Log("Log3");
     Ray ray = gameCamera.ScreenPointToRay (Input.mousePosition);
     RaycastHit hitInfo;
  Debug.Log("Log4");
      if (Physics.Raycast (ray, out hitInfo)) {

           objectToPlace.position = hitInfo.point;
           Debug.Log("Log5");

if (Input.GetKey(KeyCode.Escape))
      {
        MenuThing.SetActive(true);
        WalkButton.SetActive(true);
      PauseButton.SetActive(true);
        Destroy(gameObject);
   }
      
         if (Input.GetMouseButton(0) && CounterThing == 0)
              {
         Debug.Log("Placed");
          MenuThing.SetActive(true);
         WalkButton.SetActive(true);
      PauseButton.SetActive(true);
        objectToPlace.GetComponent<BuildingFollow2>().enabled = false;
         objectToPlace.GetComponent<RotationBuilding>().enabled = false;
         objectToPlace.GetComponent<BoxCollider>().enabled = true;
         }
  

      void OnTriggerEnter() {
        CounterThing = 1;
        Debug.Log("Collision");
   }
  }

   void OnTriggerExit() {
     CounterThing = 0;
   Debug.Log("No Collision");
    }
  }
}

Your indentation in this code snippet here is confusing, but it looks like you have the OnTriggerEnter method inside of the Update method. Just move it outside.

I fixed that but there is a error saying “a namespace cannot directly contain members such as fields or methods” on line 67

Here it is, correctly formatted:

    public Transform objectToPlace;

    private Camera gameCamera;
    private GameObject gameCamera3;
    private GameObject MenuThing;
    private GameObject Pallete;
    private GameObject WalkButton;
    private GameObject PauseButton;

    public int CounterThing = 0;

    void Start() {
        MenuThing = GameObject.FindWithTag("MenuButton");
        WalkButton = GameObject.FindWithTag("WalkButton");
        PauseButton = GameObject.FindWithTag("PauseButton");
        Pallete = GameObject.FindWithTag("Pallete");
        Pallete.SetActive(false);
    }

    void Update() {
        MenuThing.SetActive(false);
        WalkButton.SetActive(false);
        PauseButton.SetActive(false);
        Debug.Log("Log1");

        Debug.Log("Log2");
        gameCamera3 = GameObject.FindWithTag("CameraTag");
        gameCamera = gameCamera3.GetComponent<Camera>();
        Debug.Log("Log3");
        Ray ray = gameCamera.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;
        Debug.Log("Log4");
        if(Physics.Raycast(ray, out hitInfo)) {

            objectToPlace.position = hitInfo.point;
            Debug.Log("Log5");

            if(Input.GetKey(KeyCode.Escape)) {
                MenuThing.SetActive(true);
                WalkButton.SetActive(true);
                PauseButton.SetActive(true);
                Destroy(gameObject);
            }

            if(Input.GetMouseButton(0) && CounterThing == 0) {
                Debug.Log("Placed");
                MenuThing.SetActive(true);
                WalkButton.SetActive(true);
                PauseButton.SetActive(true);
                objectToPlace.GetComponent<BuildingFollow2>().enabled = false;
                objectToPlace.GetComponent<RotationBuilding>().enabled = false;
                objectToPlace.GetComponent<BoxCollider>().enabled = true;
            }
        }
    }

    void OnTriggerEnter() {
        CounterThing = 1;
        Debug.Log("Collision");
    }

    void OnTriggerExit() {
        CounterThing = 0;
        Debug.Log("No Collision");
    }

I’m assuming you have all this inside of a class, right? If not, that would be the reason for the namespace error.

I replaced the script and i got the editor to play the scene, but it just thinks it is colliding with something. If you look in the OnTriggerEnter() method, there is “CounterThing = 1”, even thoough the object with the script attached is no colliding, it says 1. Any help here? (Should i open a new thread post?)

CounterThing is public, so it should be editable in the script’s inspector.
Is it maybe set to 1 by default in the inspector?

I did those instructions (setting the public int to 1), and it is the same results, however, colliding and exiting that collision returns int to 0. So it is colliding with something first up and is still colliding with it, the only thing that would be this is the scene floor (The ground).

The floor of the scene is the problem, how do i make the floor not affect the box collider on the object (The script attached object)?

See layers and layer-based collision detection.
Create a layer for your ground and your other object, and disable interaction between the two layers from the Physics settings.
This will, however, make it so that your object will just pass through the floor. If that’s what you want, then there’s no problem. Otherwise, you’d want to take a different approach:

All of the OnCollision…/OnTrigger… methods are called when any object enters/exits the collider. So what your current OnTriggerEnter method is doing is setting CounterThing to 1 when anything touches your object. If you want to exclude the floor from affecting this value, you could give your floor a tag and then compare if the other object that was collided with has that tag.

void OnTriggerEnter(Collider collider) {
   //Check if the other object we collided with does not have a "Floor" tag.
   if(!collider.gameObject.CompareTag("Floor")) {
      CounterThing = 1;
   }
}

On a side note, I’m just now noticing that your OnTriggerEnter/Exit methods aren’t correctly defined. They are missing the Collider parameter as shown in the above example.
See their documentations here:

https://docs.unity3d.com/ScriptReference/Collider.OnTriggerExit.html

Thank you! I have tested the code, and it works. I have been working on this script for months and i finally gotten it working. Thank you!