2d Triggers are not working properly

Hello everyone,

I’m working on a fishing game in Unity where I need to set the number of fish caught (fishAmount) based on the player’s interaction with different colored areas (Green, Red, LightGreen, Orange). I’m currently using the OnTriggerStay2D method to detect the areas and update the fishAmount accordingly. However, when I call the ClickDeck method, fishAmount often holds the wrong value.

Here’s the full code for reference:

using DG.Tweening;
using Dreamteck.Splines;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class FishinDeckController : MonoBehaviour
{
    public GameObject deck;
    public Animation _animation;
    public GameObject perfectText;
    public GameObject goodText;
    public GameObject notBadText;
    public PlayerFishingController playerFishingController;
    public static int fishAmount = 0;
    public GameObject fishPrefab;
    public Animator playerAnimator;
    public Transform fishParent;
    public Transform firstStackPos;
    public Transform stackParent;
    public PlayerInteractController PlayerInteractController;
    private void OnTriggerStay2D(Collider2D other)
    {
        if (!_animation.enabled)
        {
            if (other.CompareTag("Green"))
            {
                perfectText.SetActive(true);
                fishAmount = 3;
            }
            else if (other.CompareTag("Red"))
            {
                notBadText.SetActive(true);
                fishAmount = 1;
            }
            else if (other.CompareTag("LightGreen"))
            {
                goodText.SetActive(true);
                fishAmount = 2;
            }
            else if (other.CompareTag("Orange"))
            {
                notBadText.SetActive(true);
                fishAmount = 1;
            }
        }
    }


    private void OnEnable()
    {
        deck.GetComponent<RectTransform>().DOScale(new Vector3(1, 1, 1), 1f).SetEase(Ease.Flash);
        _animation.enabled = true;
        perfectText.SetActive(false);
        goodText.SetActive(false);
        notBadText.SetActive(false);
    }
 

    public IEnumerator DeckCloser()
    {
        yield return new WaitForSeconds(.1f);
        deck.GetComponent<RectTransform>().DOScale(new Vector3(0, 0, 0), 1f).SetEase(Ease.Flash).OnComplete(() =>
        {
            deck.SetActive(false);
        });
    }


    public void FishingDeckOpener()
    {
        gameObject.SetActive(true);
        playerFishingController.isPlayerFishing = true;
    }



    public void CatchFish(int fishAmount)
    {
        playerAnimator.SetBool("didFishCame", true);

        StartCoroutine(CatchFishCoroutine(fishAmount));
    }
    private IEnumerator CatchFishCoroutine(int fishAmount)
    {
        for (int i = 0; i < fishAmount + 1; i++)
        {
            Transform fishTransform = fishParent.transform.GetChild(i);

            // SplineFollower'� devre d��� b�rak
            fishTransform.GetComponent<SplineFollower>().enabled = false;

            // Yeni bal�k instantiate et ve pozisyonunu ayarla
            GameObject newFish = Instantiate(fishPrefab, fishTransform.position, fishTransform.rotation);
            newFish.transform.SetParent(fishParent);

            // Stack Parent'ta bulunan �ocuk say�s�n� al
            int childCount = stackParent.transform.childCount;

            // E�er stack limiti dolmam��sa bal��� stack'e ekle
            if (childCount < PlayerInteractController.playerCarryLimit)
            {
                float startYPosition = 0f;
                float newYPosition = startYPosition + 0.35f * childCount;
                Vector3 newLocalPosition = new Vector3(0, newYPosition, 0);

                // Bal��� stack listesine ekle
                PlayerInteractController.stack.Add(newFish);

                // Orijinal global scale'i sakla
                Vector3 originalGlobalScale = newFish.transform.lossyScale;

                // Yeni parent'� ayarla
                newFish.transform.SetParent(stackParent.transform, true);

                // Orijinal global scale'i yeniden uygula
                newFish.transform.localScale = new Vector3(
                    originalGlobalScale.x / stackParent.transform.lossyScale.x,
                    originalGlobalScale.y / stackParent.transform.lossyScale.y,
                    originalGlobalScale.z / stackParent.transform.lossyScale.z
                );

                // Bal��� yeni pozisyona ta��
                yield return newFish.transform.DOLocalMove(newLocalPosition, 0.2f).OnComplete(() =>
                {
                    // Hareket tamamland�ktan sonra rotasyonu s�f�rla
                    newFish.transform.localRotation = Quaternion.identity;
                }).WaitForCompletion();

                // BoxCollider'� devre d��� b�rak

                // Event g�nder

                // E�er stack limiti dolmu�sa fullText'i g�ster
                if (PlayerInteractController.stack.Count == PlayerInteractController.playerCarryLimit)
                {
                    float fullTextYPosition = startYPosition + 0.35f * (childCount + 1); // childCount + 1 yeni �ocu�u da hesaba katmak i�in
                    Vector3 fullTextPosition = new Vector3(0, fullTextYPosition, 0);

                    // fullText g�sterme i�lemleri burada yap�labilir
                }
            }

            // Mevcut bal��� devre d��� b�rak (�sterseniz yok edebilirsiniz)
            // Destroy(fishTransform.gameObject); // Alternatif: Bal��� yok et

            // Her bal�k hareketinden sonra bir s�re bekle (�rne�in 0.1 saniye)
            yield return new WaitForSeconds(0.1f);
        }
    }

    public void ClickDeck()
    {
        _animation.enabled = false;
        CatchFish(fishAmount);
        StartCoroutine(DeckCloser());
    }
}

The main issue is that fishAmount doesn’t always hold the correct value when ClickDeck() is called. I suspect that the OnTriggerStay2D method is either not updating fishAmount correctly or that the arrow’s movement across the power meter causes the value to change unexpectedly.

How can I ensure that fishAmount is correctly set according to the arrow’s position on the power meter when the player clicks to proceed? Are there any best practices or different approaches to handle this scenario?

Thanks in advance for any insights or suggestions!

It is hard to draw any conclusions without more information about how your triggers are set up in the scene or how the collider on this object moves through those triggers.

I would recommend adding some Debug.Log statements to your OnTriggerStay2D function to see if it is detecting the wrong collider. I would also consider using OnTriggerEnter2D instead of OnTriggerStay2D. Is there any reason the OnTriggerStay2D code needs to run every frame even if the collision doesn’t change?