s-m-k
1
I have a weird problem that happened yesterday (probably the first time I tried to run this project with 5.1.3), but I didn’t change anything in the source code, didn’t manipulate the involved prefabs nor layer settings.
Take a look at this piece of code:
public class SawCollider : MonoBehaviour {
Saw parent;
bool isKilling;
void Awake() {
parent = GetComponentInParent<Saw>();
}
void OnTriggerEnter(Collider collider) {
Debug.Log(Time.time + ": enter -> " + collider); //works
}
void OnTriggerExit(Collider collider) {
Debug.Log(Time.time + ": exit -> " + collider); //works
}
void OnTriggerStay(Collider collider) {
Debug.Log(Time.time + ": stay -> " + collider); //doesn't work, but worked yesterday
parent.HurtCollider(collider);
}
}
Now the problem is that the OnTriggerStay function worked perfectly well all the time, but yesterday it just suddenly stopped working in 5.1.3 (updating to 5.2 didn’t help). My co-worker, with Unity 5.1.0f3, didn’t notice the problem (it works on his machine). According to docs, my OnTriggerStay function should work (all interacting objects have rigidbodies attached). The OnTriggerEnter/Exit functions (that I added today to test if physical layers are visible to each other) work like a charm too.
Here’s a log, so you can see that the difference between OnTriggerEnter and OnTriggerExit can be as high as a few seconds, so Unity should call OnTriggerStay certailny more than 0 times:

So, what’s wrong with it? An Unity bug? Out-of-date docs? Because really, I didn’t change anything, it just stopped working on my machine. I tried to test ancient revisions too (the ones where I created the saw and tested it) with no success - it doesn’t work in those as well.
Note that I don’t need a work-around solution for this, as it’s not difficult to write, I just only want to know why it stopped working.
Update: my friend updated Unity to 5.2 and it messed up the saws as well.
Update 2: Unity team answered that they’re already working on fixing this issue. Unity Issue Tracker - [TriggerCollider] OnTriggerStay is not called for child objects
s-m-k
2
OK, I solved the issue.
This is an Unity bug, as I made a simple project to isolate the issue and it happened again.
The problem appears with child colliders, i.e., colliders that have no Rigidbody attached to themselves, but are connected to the parent’s Rigidbody.
It seems that Unity recently added a feature of calling the collision event functions on parent objects (where the Rigidbody is attached). The events are called on child objects as well (in case of exit/enter), but if you don’t define the stay function in the parent, the child object won’t receive the event at all (it all works correctly when you define the function in parent and the child). So, the lack of OnTriggerStay in the parent causes the child’s OnTriggerStay to be ignored by the engine (but OnTriggerEnter/Exit works correctly).
Take a look at my example project and play with TheParentBody.cs (the commented part) to see what I mean (I use Unity 5.2.0f3). Open “scene.unity” scene and run it.
I’ll send a similar bug report to the Unity team.
[54428-staybug.zip|54428]
God Bless you, been looking for a solution for 2 days GOD SAKE papa bless u!