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

On trigger not working (Solved)

Discussion in 'Scripting' started by Adagio-81, Mar 14, 2020.

  1. Adagio-81

    Adagio-81

    Joined:
    Mar 28, 2015
    Posts:
    18
    Hi

    I know there have been tons of threads like this and I have read most of them, but none of them gives an answer to my problem. They all seems to talk about using the wrong method in 2D games or forgetting to check which situations triggers each other
    This is a 3D game

    In my game I have two objects. There's the RailroadTrackGhostStraight object (see code below), which has a rigidbody (Is Kinematic set to true) and Box Collider (Is Trigger set to true). Then I have a water object with a Box Collider (Is Trigger set to true)

    Nothing is happening when entering, leaving or staying inside

    RailroadTrackGhostStraight:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class RailroadTrackGhostStraight : RailroadTrackGhost
    4. {
    5. // A lot of code has been removed, but in neither this or the inherited class there is anything that should cause OnTriggerXXX not to be triggered
    6.     public RailroadTrackStraightRotation Rotation;
    7.  
    8.     void OnTriggerEnter(Collider other)
    9.     {
    10.         WriteDebug("OnTriggerEnter");
    11.     }
    12.  
    13.     void OnTriggerExit(Collider other)
    14.     {
    15.         WriteDebug("OnTriggerExit");
    16.     }
    17.  
    18.     void OnTriggerStay(Collider other)
    19.     {
    20.         WriteDebug("OnTriggerStay");
    21.     }
    22.  
    23. public void WriteDebug(string message)
    24.     {
    25.         Debug.Log(name + " - " + message);
    26.     }
    27. }
    To test if I had understood this triggering correctly, I decided to create a small test project with the same basic setup (one object with is kinematic rigidbody and a box collider with trigger, then one object just with the triggering box collider) and this works perfectly well
    Obviously there is something interfering in my game causing it not to trigger, but I can't see what that should be
     
  2. Gordon_G

    Gordon_G

    Joined:
    Jun 4, 2013
    Posts:
    358
    Are these objects in layers that you have unchecked in the collision matrix in the 'project settings > physics' category? If you have unchecked these for their respective layers, the triggers won't fire.

    Otherwise, Have you switched to scene view while the game is playing and directly manipulate objects to make them collide while verifying in their respective inspectors that:

    While the game is playing Is Trigger is still set to true on your Raiload object and the RigidBody is still enabled, and that the script is on the object and enabled?

    Both objects' the box colliders are enabled and the box collider sizes are set appropriately and they are not offset from the objects?
     
    Adagio-81 likes this.
  3. Adagio-81

    Adagio-81

    Joined:
    Mar 28, 2015
    Posts:
    18
    Thank you so much, I completely forgot about that one