Trouble with changing speed of my object during game.

Hello! So, I’m trying slowly to do a very simple game for start, when my square is moving left with constant speed with ability to jump. While jumping it can eat little red or green squares. After eating 3 greens the square speeds up, after eating 3 red it slows down. Now i do not know where is a mistake in my code, but while the counter of eaten squares apparently works, the “changingSpeed” method does nothing, like the condition in “if” statement was false. Can you see what’s wrong?

public class PlayerControl : MonoBehaviour {

	    public float speed = 1f;
        private int countBad = 0;
        private int countGood = 0;

 void OnTriggerEnter2D(Collider2D coll)//this works
    {
        if (coll.gameObject.tag.Equals("consumableBad"))
        {
            countBad++;
            Debug.Log(countBad);
        }

        if (coll.gameObject.tag.Equals("consumableGood"))
        {
            countGood++;
            Debug.Log(countGood);
        }
    }

    void changingSpeed()// this does not work
    {
        if (countGood == 3)
        {
            Debug.Log("true");
            speed += 10f;
        }
        if (countBad == 3)
        {
            Debug.Log("true");
            speed += -10f;
        }
    }
}

The changingSpeed does not get called inherently, that is to say, it doesn’t just happen. You must call the function with another line of code. The OnTriggerEnter gets called because unity is using it when your trigger collider enters a collision.

To get this to work you should call (that is make the code happen) the function when you have a collision. Just add the line changing speed to the end of your collision function.

  void OnTriggerEnter2D(Collider2D coll)//this works
     {
         if (coll.gameObject.tag.Equals("consumableBad"))
        {
             countBad++;
             Debug.Log(countBad);
         }
 
        if (coll.gameObject.tag.Equals("consumableGood"))
         {
             countGood++;
             Debug.Log(countGood);
         }
        changingSpeed();
    }

And, as a tip from convention of c# code writing, however not needed, functions like you void changingSpeed() are typically capitalized. Best of Luck.