Problem using Switch and Colliders Interactions

I want to switch animations when i click in a box. The idea is that when i click the box a animation start and after that the player need to get away of the box (OnColliderExit) and when the player interact again with the box clicking on it another animation starts.
The problem is that i made a counter to count the times the player clicks, and each time the OnColliderExit enter the counter increase.
BUT i have the problem that each time the player is OnColliderExit the counter increase 2 numbers, and i need that only increase one. I dont know if is something on the code or with the colliders.
If someone did this kid of interactions im open to other ways to do it .

The code:

private void OnTriggerStay(Collider other){


        if (Input.GetMouseButtonDown(0)){
         

           tapa.enabled = true;
           tapa.Play("caja_izq_abriendo");

           switch (contador) {

            case 0:

                if (other == desencadenante && Condicion == estado.TriggerStay)
                {
                    objetivo_uno.enabled = true;
                    objetivo_uno.Play("luzes_point_c");
                    source_uno.clip = track_uno;
                    source_uno.Play();
                }
          
                break;

            case 1:

                if (other == desencadenante && Condicion == estado.TriggerStay)
                {
                    objetivo_dos.enabled = true;
                    objetivo_dos.Play("perlins");
                    source_dos.clip = track_dos;
                    source_dos.Play();
                }

               
                break;


            case 2:

                if (other == desencadenante && Condicion == estado.TriggerEnter)
                {
                    objetivo_dos.enabled = true;
                    objetivo_dos.Play("cilindros_arriba");
                    source_tres.clip = track_tres;
                    source_tres.Play();
                }

               
                break;

        }

       }
    }



    private void OnTriggerEnter(Collider other)
    {

       clic.enabled = true;
       clic.Play("clic_1");
       Debug.Log(contador);

    }

     private void OnTriggerExit(Collider other)
    {

       clic.enabled = true;
       clic.Play("clic_exit");

       contador++;

       Debug.Log(contador);

        if (contador >= 3) {

            contador = 0;
        }
      


    }

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

1 Like

@Kurt-Dekker Thanks for the reply! i actually did that yes. I put it in every event, the thing is that in the event OnColliderExit, thats is where i increase the variable, i have that problem that the variable increase two times when i need that increase only one number.

Do you perhaps have more than one collider that is exiting the trigger?