Help with my OnTriggerEnter / Exit Function

Hey i have a script which starts and stops the timer in another script.

The problem is that when i leave the OnTriggerExits´ collider the first time it immediately think that i left it twice so it jumps from round 1 to 3. How do i prevent that?

Round = 1 by default

function OnTriggerEnter(collision : Collider)
{
    if(collision.gameObject.tag == "Player1" && Round == 1)
    {
        Timer.IncreaseT1A = false;
        Timer.Show1A = true;
    }
    if(collision.gameObject.tag == "Player1" && Round == 2)
    {
        Timer.IncreaseT2A = false;
        Timer.Show2A = true;
    }
    if(collision.gameObject.tag == "Player1" && Round == 3)
    {
        Timer.IncreaseT3A = false;
        Timer.Show3A = true;
}

function OnTriggerExit(collision : Collider)
{
    if(collision.gameObject.tag == "Player1" && Round == 1)
    {
        Debug.Log("We just passed from round 1 to 2");
        Timer.IncreaseT2A = true;
        Round = 2;
    }
     if(collision.gameObject.tag == "Player1" && Round == 2)
    {
        Debug.Log("We just passed from round 2 to 3");
        Timer.IncreaseT3A = true;
        Round = 3;
 
    }
}

how many collider components has your GameObject “Player1”

if it has two colliders and each enters/exits the trigger the function is called twice.

It have a Sphere collider and a rigidbody if the rigidbody counts as 1?

I have tried something else:

I will try and have 2 OnTriggerEnter functions in one script.

function OnTriggerEnter(collision : BoxCollider)

And

function OnTriggerEnter(collision : CapsuleCollider)

But now i get the error:

Script error: OnTriggerEnter
This message parameter has to be of type: Collider
The message will be ignored.

Can you explain why / what to do about it

First of all, you’ve got redundant code there. As soon as you notice you could have copy&paste your code or see that all the stuff just differs a little bit or based on some variables, you can create methods for that, shorten the code and gain better readability.

The problem in your code:

When OnTriggerExit is called and you run into the first if, the assignment ‘round=2’ will make sure that the next if’s condition evaluates to true as well.

If you want to stick with the code, use ‘else if’ in the second case.

2 Likes

and again:

else if(collision.gameObject.tag == "Player1" && Round == 3)
1 Like

I already had tried that without it working but now i just noticed that its becuse i didnt have “Timer.Show3A=true;” in it but i have it now.

Thanks man :smile:

I´m new to coding so i´m trying to make my codes as effective as possible :slight_smile:

But could you maybe give me an example of how it would look if it should be less redundant so i can take it with me in my next code? ^^

Well, if you look at your code, you’ll see that you check the tag in every case. What about only checking it once and go through any other condition inside the if? Nesting if statements is equal to the conjunction (‘&&’) of conditions.
That’s not a must, but it is often better to read.
As i don’t know your implementation of your timer, i cannot give any hint on that but it looks as if it could be improved as well. If so, you could have a more general yet more dynamic code which would not require lot’s of changes in case you add more rounds. You may not need that, but if you think about how to generalize it, you’ll improve the ability to find more efficient, more maintainable and shorter solutions and algorithms, which is what you want as soon as your project gets bigger.

(Regarding tags i also recommend to make a class that simply holds constant string variables which represent your tags. Once defined, use the variable instead of a string literal in order to avoid typos and to keep your code easy to maintain. Imagine you rename one tag, and had to change it in a bigger project in every single file and function - it’s way easier to change it just once which applies automatically to everything.)

Thanks man!