0

In the fishing mini game I'm working on, each unique fish that the player catches is written to a "journal" type UI. If the player has caught that type again, instead of writing a whole new entry to the UI, a score increments by 1 to show how many times they've caught that type of fish.

I'm using scriptable objects to store the information about each fish (name, size, a picture, etc) along with the number of times it's been caught. I can see in the inspector that the variable is increasing each time that fish is caught and I'm also debugging a message each time a unique fish has been caught as well as the number of times.

The UI seems to update the number of times caught the first time, however, however it seems to always stay at 1 for every time that fish is caught afterwards, even though the variable that I'm writing to the UI is changing.

Here's the code that updates the UI:

public void UpdateFishPages(Fish fish)
{
    if (!CheckIfExists(fish))
    {
        Debug.Log("New Fish: " + fish.fishName + " " + "caught ");
        Debug.Log("Fish index: " + fishIndex);
        fishCaught.Add(fish);
        fishes[fishIndex].SetActive(true);
        fishes[fishIndex].GetComponent<Image>().sprite = fish.image;

        fishes[fishIndex].transform.GetChild(0).transform.GetChild(0).GetComponent<TMPro.TextMeshProUGUI>().text = fish.fishName;
        fishes[fishIndex].transform.GetChild(0).transform.GetChild(1).GetComponent<TMPro.TextMeshProUGUI>().text = fish.fishDesc;
        fishes[fishIndex].transform.GetChild(0).transform.GetChild(2).GetComponent<TMPro.TextMeshProUGUI>().text = fish.fishSize;

        string newFishCount = fish.timesCaught.ToString();
        Debug.Log(newFishCount);
        fishes[fishIndex].transform.GetChild(1).transform.GetChild(0).GetComponent<TMPro.TextMeshProUGUI>().text = newFishCount;
        fishIndex++;
    }
    else
    {   


        string newFishCount = fish.timesCaught.ToString();
        Debug.Log(fish.fishName + " now caught " + fish.timesCaught + " times!");

        fishes[fishIndex].transform.GetChild(1).transform.GetChild(0).GetComponent<TMPro.TextMeshProUGUI>().text = newFishCount;

    }
}

bool CheckIfExists(Fish fish)
{
    if (fishCaught.Contains(fish))
    {
        return true;
    }
    else
    {
        return false;
    }
}

and here's how I'm calling that method when a player catches the fish:

if (Input.GetKeyDown(KeyCode.E) || Input.GetButtonDown("Button A"))
            {
                if (timer < 1f)
                {
                    int randomFish = Random.Range(1, 5);
                    playerAnimator.SetInteger("fishing_randomIndex", randomFish);

                    fish[randomFish - 1].timesCaught++;

                    journal.UpdateFishPages(fish[randomFish - 1]);

                    //Debug.Log("caught");
                    timer = 0;
                    fishStage = 2;
                }
            }
            else if (timer > 1f)
            {
                //Debug.Log("Didn't reel");
                playerAnimator.SetTrigger("fishing_fail");

            }

I'm hoping someone can shed some light onto what I might be doing wrong.

Many thanks for your time and help!

  • Could you include your classes .. it is a bit hard to see how your snippets are connected exactly – derHugo May 13 '20 at 14:00
  • I could send the whole script but it's very long and has lots of unrelated stuff. The `UpdateFishPages()` is part of the Journal class which is attached to a UI element in Unity. The if statement is part of a larger check within an Interactions class that contains all the logic for fishing, as well as other interactions in my game. – Jamie Blacknell May 13 '20 at 14:07

0 Answers0