- Home /
OnTriggerEnter, OnTriggerExit Work Only Once
I'm having a problem with the my triggers not being triggered more than once.
I have three types of objects which are instantiated and put into three pre-existing parents. Each of the three objects are instantiated multiple times (with unique names) and all put into their appropriate parents. Each has a collider (with Is Trigger selected) and a rigidbody attached. Example Hierarchy:
OverallParent
-Sides
---Side0
---Side1
---Side2
-Guns
---Gun0
---Gun1
---Gun2
-Selections
---Selection0
---Selection1
---Selection2
There is then one large collider set to Is Trigger, also with a rigidbody attached, which acts as the boundary of the playable area. If one of the instantiated objects goes outside, it is Destroyed.
However the both the OnTriggerEnter and OnTriggerExit only work for the first instantiated object which enters or leaves. All others are simply ignored for some reason. I have verified multiple times that every one of them has the same collider, rigidbody, and all other settings both before and after the first trigger occurs.
Does anyone have any ideas?
can you build a sharable project that reproduces this issue? It would help us perhaps identify the problem, and maybe the process of rebuilding a simple example that exhibits this behavior might help you see it yourself.
I'll see if I can put a sample together. But in the meanwhile, I think something really wonky is happening in Unity for me. I'm still trying to track down this problem and noticed something very strange. Here's a snipped of code.
.....
} else {
print("true");
print("upwards randomSideSize = "+randomSideSize);
startPosition = startPosition - Vector3(0,randomSideSize,0); // Set new start position for next instantiation
}
.....
$$anonymous$$y ter$$anonymous$$al output is as follows:
1) upwards randomSideSize = 3.550462 UnityEngine.$$anonymous$$onoBehaviour:print(Object)
2) A newSide position = (7.0, -46.9, -14.6) UnityEngine.$$anonymous$$onoBehaviour:print(Object)
What's really strange here is that the line 'print("true");' is completely ignored!? I've double clicked #1 in the console to be absolutely sure it was indeed the line directly after the print("true") statement. I really don't understand what's going on here but I have a feeling it's somehow related to my problem. In 20 years of coding I've never seen something quite like this.
is this snippet of code happening inside of a loop? A full project would really be helpful =], sorry I can't do more with just this much information. I agree something definitely looks wonky. If you push something to git let me know, I wouldn't $$anonymous$$d giving you a second pair of eyes.
Answer by sparkzbarca · Mar 15, 2013 at 06:17 AM
your trigger shouldnt be the playable area it should be 4 areas outside of the area.
You define where you dont want them to go you dont spawn them inside the trigger
and use ontriggerexit.
I don't think triggers track multiple objects. they pass a single object to the function OnTriggerEnter not a list. I'm guessing that makes it impossible to track multiple objects but i'm not sure.
Thanks sparkzbarca. It looks like doing it the opposite way around fixed my problem. I'm guessing that when multiple objects are instantiated within the same frame, within a collider, that the collider doesn't register each individually.
Answer by Eric5h5 · Mar 15, 2013 at 07:07 AM
Turn off "collapse" in the console, or if you're using Unity 4, keep collapse on and pay attention to the line count on the right.
Your answer
Follow this Question
Related Questions
OnTriggerExit doesn't fire when changing RigidBody (bug? feature?) 2 Answers
OnTriggerExit happens too soon! 1 Answer
OnTriggerEnter/Exit called unexpectedly 2 Answers
OnTriggerEnter works, OnTriggerExit doesn't 3 Answers
OnTriggerEnter function in c# 0 Answers