- Home /
OnTriggerEnter doesn't work as it should? (c#)
Hi everyone, I am facing a problem with OnTriggerEnter.
Why does OnTriggerEnter work at start, but after it has runned as it should and you change variables for the conditions, it doesn't work? The methods that sets/changes the variables are the same for the start condition, and when event is triggered
I have debugged to check that the variables are actually changed to what we want, and that happens correctly. I am checking for collision detection for up to 4 conditions, four different objects colliding with the Collider. Basically you have four version of the code below, with four variable for each(activeCell2, activeBodyPart2, danceMove2Completed, currentDanceMove2 etc)
The variables are changed correctly, but they aren't beeing checked in OnTriggerEnter, even though they should have.
Thank you and apologize for a little messy code.
void OnTriggerEnter (Collider cell)
{
if (cell.name == "cellID " + activeCell && this.name == activeBodyPart && !danceMoveCompleted) {
/**< if the active cell and our active bodypart collides, and dancemoves(so we can set currentMove to next dancemove when a dancemove is complted)
* not completed and a dance is not completed*/
cell.renderer.enabled = false; // after every collision, make cells invisible/dissapear
if (counter < currentDanceMove.Length - 1) {
drawFirstInstruction (currentDanceMove, counter);
}
if (counter < currentDanceMove.Length - 2) {
drawSecondInstruction (currentDanceMove, counter);
}
if (counter < currentDanceMove.Length - 3) {
drawThirdInstruction (currentDanceMove, counter);
}
Debug.Log (activeBodyPart + "collided with " + cell.name + " Counter: " + counter);
if (counter < currentDanceMove.Length - 1) { /**< increment counter by one while counter is less than second last element of the array*/
counter++;
}
if (activeCell == currentDanceMove [currentDanceMove.Length - 1]) { // if dance move is completed and dance is not completed
danceMoveCompleted = true;
checkMoves (numOfabp);
danceMoveCompleted = false;
danceMoveCompleted = true;
} // end if
activeCell = currentDanceMove [counter]; // active cell is set to the incrementing counter variable in currentDanceMove array for every collision
} // end first if
Print your vars as you enter the OnTrigger. Probably a mispelling or extra letters. If it does print, you know the OnTrigger is fine, and it's your code. If it doesn't print, then you know OnTriggerEnter isn't firing (maybe you never fully leave the trigger?)
1st line OnTrigger: Debug.Log("name="+cell.name+" wantC="+activeCell);.
If needed, same with activeBodyPart and this.name.
You are right in that, I had stated a print inside ontriggerenter earlier, and this did run. Problem is that when the variables in the conditions for the ontriggerenter are changed, that block doesn't run anymore, even when the conditions are met.
Are you sure you are leaving the trigger fully? Try running: Debug.Log("Trigger: " + cell.name);
Just after: void OnTriggerEnter (Collider cell) {
To see if you actually go back inside? Then, after that, if it does, try debugging by check the values one by one, so try each one of the conditions by itself, until you find the one that stops it from firing :).
Thank you for reply, I tried that and it did go back inside. So I tried to debug this statement: At first activeCell is 54, this is changed to 55 and when the leftElbowCube object collides with activeCell 55 it actually prints out: "BodyPart collided: leftElbowCube" "activeBodyPart: leftHandCube" "activeCell: 54"
Which is strange, as the activeBodyPart should have been leftElbowCube, and activeCell should have been 55
void OnTriggerEnter (Collider cell)
{
if (cell.name == "cellID " + activeCell && this.name=="leftElbowCube" ) {
Debug.Log ("Bodypart collided: " + this.name);
Debug.Log ("Activebodypart: " + activeBodyPart);
Debug.Log ("ActiveCell: " + activeCell);
}
The conditions are probably not being met -- an extra space, etc... . Try testing each one directly, for example:
if(cell.name == "cellID " + activeCell) Debug.Log("same cell"); else Debug.Log("diff cell");
Answer by alizan · May 08, 2013 at 03:25 PM
The variables inside OnTriggerEnter doesn't seem to change, even though they are changed outside OnTriggerEnter which the debug statements are showing.
Answer by Owen-Reynolds · May 09, 2013 at 02:40 PM
RE: comment about solving using static vars
So your trigger script has got string activeCell; as a global, resulting in 4 copies of activeCell (one for each trigger)? And instead, there should be a single copy they all share? Static vars are one way to do that, but they almost always cause problems later on (for example, you want to add a 2nd dancer.)
Instead of statics, can create one a "I'm the dancer" script, with all the common data and routines for one dancer: Ex:
// dancerScript, for one dancing thing
public string activeCell;
public void drawFirstInstructions() {...}
Then have the triggers all refer to that dancer:
// triggerScript:
public dancerScript DS; // drag object w/dancerScript here
void OnTriggerEnter(...) {
if(DS.activeCell == ... ) DS.drawFirstInstruction();
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
OnTriggerEnter triggering twice on one collider? 5 Answers
AI Attack Not Working! W/Video 1 Answer
Strange OnTriggerEnter/Exit() Behavior 0 Answers