OnTriggerExit does not seem to be functioning, does not stop my coroutine

Ok so I have made a method where, when the player touches a collider (flowers), they get pollen every second. I have made an OnTriggerEnter for entering the collider and triggering the coroutine method, and an OnTriggerExit for stopping the pollen every second.

However, once the player stops touching the flower collider, the pollen count continues to increase! I cannot figure out how to get the pollen count to stop working once the player isn’t touching the flowers. I have also noted that when moving to another flower, the coroutine fires again, making the pollen go up faster and that shouldn’t be happening either. I have made sure all the layers were named correctly, and all the colliders needing to be triggers were checked.

Any suggestions would be super helpful!

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class AddPollenScript : MonoBehaviour
{
    [SerializeField] int givePollen = 1;
    int pollen = 0;

    Collider2D flowerCollider;
    [SerializeField] Text pollenText;

    // Start is called before the first frame update
    void Start()
    {
        flowerCollider = GetComponent<Collider2D>();
        UpdatePollenText();
    }

    // Update is called once per frame
    void Update()
    {
        //AddPollen();
        //StopAddingPollen();        
    }

    private void OnTriggerEnter2D(Collider2D other)
    {        
        if (flowerCollider.IsTouchingLayers(LayerMask.GetMask("Good Bees", "Player")))
        {
            StartCoroutine(GivePollenPlayer());
            Debug.Log("Touching Good Bee.");
        }
        if (flowerCollider.IsTouchingLayers(LayerMask.GetMask("Evil Bees")))
        {
            StartCoroutine(GivePollenEnemy());
            Debug.Log("Touching Evil Bee");
        }
    }

    private void OnTriggerExit2D(Collider2D other)
    {
        if (!flowerCollider.IsTouchingLayers(LayerMask.GetMask("Good Bees", "Player")))
        {
            StopCoroutine(GivePollenPlayer());
            Debug.Log("Touching Good Bee.");
        }
        if (!flowerCollider.IsTouchingLayers(LayerMask.GetMask("Evil Bees")))
        {
            StopCoroutine(GivePollenEnemy());
            Debug.Log("Touching Evil Bee");
        }
    }

    private IEnumerator GivePollenPlayer()
    {
        while (true)
        {
            pollen += givePollen;
            UpdatePollenText();
            yield return new WaitForSeconds(1);
        }
    }

    private IEnumerator GivePollenEnemy()
    {
        throw new NotImplementedException();
    }

    private void UpdatePollenText()
    {
        pollenText.text = pollen.ToString();
    }
}

For controlled Start and Stop of Coroutines string references are not optimal. (I would say string references are always a mess… :wink: )
Try sth. like

Coroutine coroutine;
void OnTriggerEnter2D(Collider2D other){
    if(coroutine != null) return; //check if croutine is already running)
    coroutine = GivePollenPlayer();
    StartCoroutine(coroutine);
}

void OnTriggerExit2D(Collider2D other){
    if(coroutine == null) return; //classic nullref escape
    StopCoroutine(coroutine);
    coroutine = null;
}

untested code :wink: