Problem with thirst

Hello everybody! I’m making a survival game on Unity3D. I have a problem with the following script

using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.Characters.FirstPerson;
public class HealthScript : MonoBehaviour
{
    public int health = 100;
    public int hunger = 100;
    public int thirst = 100;
    // Start is called before the first frame update
    void Start()
    {
        InvokeRepeating("DecreaseHunger", 20.0f, 20.0f);
        InvokeRepeating("DecreaseThirst", 15.0f, 15.0f);
    }
    
    void DecreaseHunger()
    {
        GameObject thePlayer = GameObject.Find("FPSController");
        CharacterController controller = thePlayer.GetComponent<CharacterController>();
        FirstPersonController playerRun = thePlayer.GetComponent<FirstPersonController>();
        float overallSpeed = controller.velocity.magnitude;
        if (hunger > 0)
        {
            if (playerRun.m_IsWalking == false)
            {
                if (overallSpeed > 0)
                    hunger -= 4;
                else
                    hunger -= 1;
            }
            else
            {
                if (overallSpeed > 0)
                    hunger -= 2;
                else
                    hunger -= 1;
            }
            GameObject hungerbar = GameObject.Find("HungerText");
            Text hungertext = hungerbar.GetComponent<Text>();
            hungertext.text = hunger.ToString();
        }
    }
    void DecreaseThirst()
    {
        GameObject thePlayer = GameObject.Find("FPSController");
        CharacterController controller = thePlayer.GetComponent<CharacterController>();
        FirstPersonController playerRun = thePlayer.GetComponent<FirstPersonController>();
        float overallSpeed = controller.velocity.magnitude;
        if (thirst > 0)
        {
            if (playerRun.m_IsWalking == false)
            {
                if (overallSpeed > 0)
                    thirst -= 4;
                else
                    thirst -= 1;
            }
            else
            {
                if (overallSpeed > 0)
                    thirst -= 2;
                else
                    thirst -= 1;
            }
            GameObject thirstbar = GameObject.Find("ThirstText");
            Text thirsttext = thirstbar.GetComponent<Text>();
            thirsttext.text = thirst.ToString();
        }
    }
}

For some reason decreasing Hunger works, but decreasing thirst doesn’t.
Thirst var is decreasing, but there are not output to the UI.Text.
Thank you all.

Hi!

Since your DecreaseHunger() is working, but not DecreaseThirst() and they are actually similar in implementation terms and also the integer indeed is decreasing I would simply recommend you to try it out with another GameObject with Text component, because otherwise it seems like some other script in your scene is overriding the value what prevent it from changing.


Also just advice for performance - since you are using these functions constantly I would not recomment to use .Find function but store core references into public variables. Because .Find essentialy loops through all existing GameObjects. If you don’t like them to be assigned in Inspector you can Find them once in Start method and then cache to prevent re-finding, since you already did that.