OnTriggerEnter triggered only once

Hi, I’m hitting my head over this for a while now, so maybe someone of you has an explanation. I’m working on a waypoint map for my enemies. All seems to be fine: I have a gameobject with a spherecollider (waypoint sensor) used to track waypoints. It’s a child of the actual navmesh agent. I move the waypoint sensor to the waypoint to follow. Once the agent collides with the sensor I know it reached the destination, so I plot a new one, move the sensor to the next waypoint and set a new path for the agent. Colliders are set both to trigger. All good… the first time. When the agent hits the second waypoint, for some reason OnTriggerEnter is not being called. And I can’t figure out why.

Any help with this?
Thanks.
M.

Here’s how the game object looks like:
9482968--1333813--upload_2023-11-20_15-55-27.png

AI Target trigger is my collider

There are very specific well-documented requirements for these callbacks. Work through them in the online documentation. They absolutely do work so you must not be meeting some requirement.

Beyond that, this is just another simple case of…

Time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Here is how you can begin your exciting new debugging adventures:

You must find a way to get the information you need in order to reason about what the problem is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the names of the GameObjects or Components involved?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

If your problem is with OnCollision-type functions, print the name of what is passed in!

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

If you are looking for how to attach an actual debugger to Unity: https://docs.unity3d.com/2021.1/Documentation/Manual/ManagedCodeDebugging.html

“When in doubt, print it out!™” - Kurt Dekker (and many others)

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.

If you find out some more and are still unable to figure out what is happening…

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

  • Do not TALK about code without posting it.
  • Do NOT post unformatted code.
  • Do NOT retype code. Use copy/paste properly using code tags.
  • Do NOT post screenshots of code.
  • Do NOT post photographs of code.
  • Do NOT attach entire scripts to your post.
  • ONLY post the relevant code, and then refer to it in your discussion.

Dude, code is very simple, and it’s working, only once and not always as it should. I already went through the docs and there’s nothing wrong. But thanks

That’s not how the Game Object look, that’s the hierarchy and it provides no useful information to this problem. The hierachy is nothing to do with physics callbacks.

No idea of your experience but navmesh/AI has no impact on things like trigger callbacks. If you know how they work then there’s no reason AI would change that. Have you set-up a scene with two triggers setup the same and tried to see if you get a callback? Ignore the AI stuff, it’s irrelevant.

Alternately, post the GameObjects in question i.e the physics components and their settings and the script that has this trigger callback.

Static Colliders never interact with Static Colliders btw in-case that’s what you’re doing.

https://docs.unity3d.com/2023.3/Documentation/Manual/CollidersOverview.html

Hard to figure the test out but try outputting something when you get OnTriggerStay and OnTriggerExit too.

That’s actually a broad category of bug, the “works only once” bug type.

You’re welcome to use other methods than debugging if you like, I certainly won’t stop you.

I’m just telling you how I would investigate, which is coincidentally the same way that any member on my team would do it, but by all means, you do you.

I’m not talking about navmesh, I’m talking about 2 collidres that collide once and then they don’t collide anymore. I described the context of the error. The main point is :

  1. I have OnTriggerEnter in the monobehaviour attached to the AI agent (the enemy).
  2. I have another collider on the waypoint sensor.
  3. I move the waypoint sensor every time its collider triggers the event
  4. It happens only once, I move the sensor to another position but when the nav agent reaches that position the colliders seem not to collide anymore

I posted the hierarchy because I maybe there’s something about the objects in it that’s not good.

Actually I had the same intuition and tried adding OnTriggerStay and exit but they are not triggered either. I’ll post the components in the objects.

Sorry, didn’t mean to be rude. I appreciate your help. But I’m here after going through all that :slight_smile:
I don’t post the code because it’s scattered all around and it’s really simple. Also because I’m using PandaBT for my AI behaviour, but it’s working fine. However if you guys need it I’ll post it

These are the components in my AI agent.
Before you mention it: the animator controller is not set because I’m using Animancer.
And this is the code in OnTriggerEnter:

private void OnTriggerEnter(Collider other)
        {
            if (waypointCollider == null || other != waypointCollider) return;
            DestinationReached = true;
            ClearTarget();
            Debug.Log("Target Cleared!!");
        }

When I add a debug breakpoint in the first line it gets hit only the first time (on the first waypoint)

And this is the waypoint sensor
9483391--1333933--upload_2023-11-20_18-48-25.png

You said:

So you are, you even mentioned it above and posted an image of the component which is why I was informing you that it’s not related so you can discount mentioning it and thinking about it from your investigation. I have no idea about what you’ve done so far to debug stuff so I have to say it.

And you’ve tried disabling the animator and navmesh-agent and just moving the GameObject over those other sensors and you still don’t get a callback? As part of debugging this, I’m seeing you say things like:

… so it’s definately not related because you’ve tested that or are just assuming it’s not?

Bisecting the problem is the only way here by removing components and behaviours until it changes something. There’s no, “it’s because you’ve not checked X” unfortunately that I can see.

Does OnTriggerStay work when OnTriggerEnter doesn’t?

By the looks of it no.

Hi guy, I solved it. It was nothing more than a stupid factor: the waypoint collider was in a position that, for a reason I still don’t get, was not reachable by the NavAgent (just by millimeters). It LOOKED like the collides were touching, but when I (by chance) zoomed in, thew were not by just a small fractional amount.

All is good what ends good :smile:

2 Likes