- Home /
Trigger Enter and Exit not working properly?
Update: I have tried doing it all over again, this is still happening. Any other ideas?
Hello,
As an awful way to double check for a collision, I've created 4 colliders(divided in 4 directions) attached to an empty gameobject.
Each of the object turns a boolean from the empty gameobject script - true with OnTriggerEnter and false OnTriggerExit.
When all of them are true something (not sure what yet) is going to happen.
The problem is the following: while my AI is moving (so are the colliders) and if they move quickly they cancel each other somehow that they are unable to be all true although all of them are colliding with the triggers.
Here is an example of the code I use on one of the colliders:
function OnTriggerEnter(hit:Collider)
{
if(hit.collider.tag == "Fence")
{
var gameObject = GameObject.FindGameObjectWithTag("DoubleCheck");
var script = gameObject.GetComponent(DoubleCheckAI);
script.topDcheck = true;
}
}
function OnTriggerExit(hit:Collider)
{
var gameObject = GameObject.FindGameObjectWithTag("DoubleCheck");
var script = gameObject.GetComponent(DoubleCheckAI);
script.topDcheck = false;
}
Notes: I have kinematic rigidbodies attached on the colliders. I have the colliders set to isTrigger.
Any help on this would be greatly appreciated because I've never encountered this until now...
EDIT (Scenario information): Certainly.
My AI is moving constantly (currently the AI is a sphere with a sphere collider).
Attached to my AI I have an empty game object with a cube collider to count the number of fences. Also attached to the AI I am doing a double check of the collision using 4 box colliders (for 4 directions) - these are the problem.
The fences are prefabs instantiated on click + drag in a certain direction.
The idea of the game is to capture the AI between 4 fences - that is what isn't working for me at all times.
Due to the issue with the double check I am not always seeing that the AI is captured if it moves. If my AI would be static if wouldn't be a problem as I've tried moving the colliders manually while pausing the unity player.
Not sure what to do next...
It is worth noting that you would be far better off to find and store the script component you are using in the Start() method. Doing this every collision will eat up performance.
Yes I am aware of that, I just wrote a mock-up code to see if my double check system works and surprise surprise this is happening to me :). Could it be a Unity Bug?
Can you better describe your scenario? What objects are moving? What is this script attached to? What is "fence"? Your code looks fine, so the problem is with the interaction.
Added after Edit, hope that information clarifies things better :). Also, I know that my method is messy but it's the most effective for what I'm trying to achieve.
Answer by theAfrican · Jul 09, 2013 at 09:47 PM
looks like you're overriding gameObject with var gameObject. rename it to var gObject and try again. gameObject is a reserved name you shouldnt override it.
try
function OnTriggerEnter(hit:Collider)
{
if(hit.collider.tag == "Fence")
{
var gObject = GameObject.FindGameObjectsWithTag("DoubleCheck");
if (gObject==null) return;
for (var n in gObject)
{
var script = n.GetComponent(DoubleCheckAI);
script.topDcheck = true;
}
}
}
function OnTriggerExit(hit:Collider)
{
var gObject = GameObject.FindGameObjectsWithTag("DoubleCheck");
if (gObject==null) return;
for (var n in gObject)
{
var script = n.GetComponent(DoubleCheckAI);
script.topDcheck = false;
}
}
Sorry mate, does not help. The issue still happens...
for the initiative anyway, thanks :)
did u mean GameObject.FindGameObjectsWithTag()?, urs is GameObject.FindGameObjectWithTag Description Returns a list of active GameObjects tagged tag. Returns null if no GameObject was found. so, shouldnt you be looping through that?
just edited my answer, try it out
No, i know the difference between object and object*s*. I am finding the object that contains the script. No need to find multiple objects as there is only one with the "DoubleCheck" tag in the whole scene.
Answer by MrPhil · Apr 17, 2016 at 08:35 PM
Two ideas:
1) Any chance you've mixed up the 3D and 2D version of these methods and components? I do that all the time.
Rigidbody Vs Rigidbody2D
Collider.OnTriggerEnter(Collider) Vs Collider2D.OnTriggerEnter2D(Collider2D)
Collider.OnCollisionEnter(Collision) Vs Collider2D.OnCollisionEnter2D(Collision2D)
BoxCollider Vs BoxCollider2D
etc.
2) Another thing that happens is you have the OnCollisionEnter2D setup in code, but you set the BoxCollider2D to be a Trigger, so you should be using the OnTriggerEnter2D in code.
Your answer
Follow this Question
Related Questions
How to make collision check if other collider has component 1 Answer
Using a Single Collider to Detect Collisions with Multiple Colliders 3 Answers
Check if trigger collider is touching other trigger collider 2 Answers
Find colliders after collision? 2 Answers
Having problems with 2d collision triggers (javascript) 1 Answer