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

OnTriggerEnter is not responding

Discussion in 'Scripting' started by cate5171, Nov 1, 2020.

  1. cate5171

    cate5171

    Joined:
    Jan 5, 2020
    Posts:
    101
    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)
    Code (CSharp):
    1. public Transform objectToPlace;
    2.  
    3.   private Camera gameCamera;
    4.   private GameObject gameCamera3;
    5.   private GameObject MenuThing;
    6.   private GameObject Pallete;
    7.   private GameObject WalkButton;
    8.   private GameObject PauseButton;
    9.  
    10.   public int CounterThing = 0;
    11.  
    12. void Start()
    13. {
    14.  
    15. MenuThing = GameObject.FindWithTag("MenuButton");
    16. WalkButton = GameObject.FindWithTag("WalkButton");
    17. PauseButton = GameObject.FindWithTag("PauseButton");
    18. Pallete = GameObject.FindWithTag("Pallete");
    19. Pallete.SetActive(false);
    20. }
    21.  
    22. void Update() {
    23.  
    24.       MenuThing.SetActive(false);
    25.       WalkButton.SetActive(false);
    26.       PauseButton.SetActive(false);
    27.   Debug.Log("Log1");
    28.    
    29.       Debug.Log("Log2");
    30.        gameCamera3 = GameObject.FindWithTag("CameraTag");
    31.        gameCamera = gameCamera3.GetComponent<Camera>();
    32.     Debug.Log("Log3");
    33.      Ray ray = gameCamera.ScreenPointToRay (Input.mousePosition);
    34.      RaycastHit hitInfo;
    35.   Debug.Log("Log4");
    36.       if (Physics.Raycast (ray, out hitInfo)) {
    37.  
    38.            objectToPlace.position = hitInfo.point;
    39.            Debug.Log("Log5");
    40.  
    41. if (Input.GetKey(KeyCode.Escape))
    42.       {
    43.         MenuThing.SetActive(true);
    44.         WalkButton.SetActive(true);
    45.       PauseButton.SetActive(true);
    46.         Destroy(gameObject);
    47.    }
    48.      
    49.          if (Input.GetMouseButton(0) && CounterThing == 0)
    50.               {
    51.          Debug.Log("Placed");
    52.           MenuThing.SetActive(true);
    53.          WalkButton.SetActive(true);
    54.       PauseButton.SetActive(true);
    55.         objectToPlace.GetComponent<BuildingFollow2>().enabled = false;
    56.          objectToPlace.GetComponent<RotationBuilding>().enabled = false;
    57.          objectToPlace.GetComponent<BoxCollider>().enabled = true;
    58.          }
    59.  
    60.  
    61.       void OnTriggerEnter() {
    62.         CounterThing = 1;
    63.         Debug.Log("Collision");
    64.    }
    65.   }
    66.  
    67.    void OnTriggerExit() {
    68.      CounterThing = 0;
    69.    Debug.Log("No Collision");
    70.     }
    71.   }
    72. }
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    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.
     
  3. cate5171

    cate5171

    Joined:
    Jan 5, 2020
    Posts:
    101
    I fixed that but there is a error saying "a namespace cannot directly contain members such as fields or methods" on line 67
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Here it is, correctly formatted:
    Code (CSharp):
    1.     public Transform objectToPlace;
    2.  
    3.     private Camera gameCamera;
    4.     private GameObject gameCamera3;
    5.     private GameObject MenuThing;
    6.     private GameObject Pallete;
    7.     private GameObject WalkButton;
    8.     private GameObject PauseButton;
    9.  
    10.     public int CounterThing = 0;
    11.  
    12.     void Start() {
    13.         MenuThing = GameObject.FindWithTag("MenuButton");
    14.         WalkButton = GameObject.FindWithTag("WalkButton");
    15.         PauseButton = GameObject.FindWithTag("PauseButton");
    16.         Pallete = GameObject.FindWithTag("Pallete");
    17.         Pallete.SetActive(false);
    18.     }
    19.  
    20.     void Update() {
    21.         MenuThing.SetActive(false);
    22.         WalkButton.SetActive(false);
    23.         PauseButton.SetActive(false);
    24.         Debug.Log("Log1");
    25.  
    26.         Debug.Log("Log2");
    27.         gameCamera3 = GameObject.FindWithTag("CameraTag");
    28.         gameCamera = gameCamera3.GetComponent<Camera>();
    29.         Debug.Log("Log3");
    30.         Ray ray = gameCamera.ScreenPointToRay(Input.mousePosition);
    31.         RaycastHit hitInfo;
    32.         Debug.Log("Log4");
    33.         if(Physics.Raycast(ray, out hitInfo)) {
    34.  
    35.             objectToPlace.position = hitInfo.point;
    36.             Debug.Log("Log5");
    37.  
    38.             if(Input.GetKey(KeyCode.Escape)) {
    39.                 MenuThing.SetActive(true);
    40.                 WalkButton.SetActive(true);
    41.                 PauseButton.SetActive(true);
    42.                 Destroy(gameObject);
    43.             }
    44.  
    45.             if(Input.GetMouseButton(0) && CounterThing == 0) {
    46.                 Debug.Log("Placed");
    47.                 MenuThing.SetActive(true);
    48.                 WalkButton.SetActive(true);
    49.                 PauseButton.SetActive(true);
    50.                 objectToPlace.GetComponent<BuildingFollow2>().enabled = false;
    51.                 objectToPlace.GetComponent<RotationBuilding>().enabled = false;
    52.                 objectToPlace.GetComponent<BoxCollider>().enabled = true;
    53.             }
    54.         }
    55.     }
    56.  
    57.     void OnTriggerEnter() {
    58.         CounterThing = 1;
    59.         Debug.Log("Collision");
    60.     }
    61.  
    62.     void OnTriggerExit() {
    63.         CounterThing = 0;
    64.         Debug.Log("No Collision");
    65.     }
    I'm assuming you have all this inside of a class, right? If not, that would be the reason for the namespace error.
     
  5. cate5171

    cate5171

    Joined:
    Jan 5, 2020
    Posts:
    101
    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?)
     
  6. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    CounterThing is public, so it should be editable in the script's inspector.
    Is it maybe set to 1 by default in the inspector?
     
  7. cate5171

    cate5171

    Joined:
    Jan 5, 2020
    Posts:
    101
    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).
     
  8. cate5171

    cate5171

    Joined:
    Jan 5, 2020
    Posts:
    101
    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)?
     
  9. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    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.
    Code (CSharp):
    1. void OnTriggerEnter(Collider collider) {
    2.    //Check if the other object we collided with does not have a "Floor" tag.
    3.    if(!collider.gameObject.CompareTag("Floor")) {
    4.       CounterThing = 1;
    5.    }
    6. }
    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.OnTriggerEnter.html
    https://docs.unity3d.com/ScriptReference/Collider.OnTriggerExit.html
     
  10. cate5171

    cate5171

    Joined:
    Jan 5, 2020
    Posts:
    101
    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!