Click here to Skip to main content
15,039,359 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I'm trying to make a survival game but i keep getting "(37,9) ThirstSlider does not exist in current context" and "(88,9) ThirstSlider does not exist in current context"

what should i do?

Code:

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

public class PlayerStats : MonoBehaviour
{
    public float Health;
    public float healthOverTime;

    public float Stamina;
    public float staminaOverTime;

    public float Hunger;
    public float hungerOverTime;

    public float Thirst;
    public float thirstOverTime;


    public Slider HealthSlider;
    public Slider StaminaSlider;
    public Slider HungerSlider;
    public Slider ThirstSilder;

    public float minAmount = 5f;
    public float sprintSpeed = 5f;

    Rigidbody myBody;

    private void Start()
    {
        myBody = GetComponent<rigidbody>();
        HealthSlider.maxValue = Health;
        StaminaSlider.maxValue = Stamina;
        HungerSlider.maxValue = Hunger;
        ThirstSlider.maxValue = Thirst;

        updateUI();
    }

    private void Update()
    {
        CalculateValues();
    }

    private void CalculateValues()
    {
        Hunger -= hungerOverTime * Time.deltaTime;
        Thirst -= thirstOverTime * Time.deltaTime;

        if(Hunger<= minAmount || Thirst <= minAmount)
        {
            Health -= healthOverTime * Time.deltaTime;
            Stamina -= staminaOverTime * Time.deltaTime;
        }

        if(myBody.velocity.magnitude>=sprintSpeed && myBody.velocity.y == 0)
        {
            Stamina -= staminaOverTime * Time.deltaTime;
            Hunger -= hungerOverTime * Time.deltaTime;
            Thirst -= thirstOverTime * Time.deltaTime;
        }
        else
        {
            Stamina += staminaOverTime * Time.deltaTime;
        }

        if (Health <= 0)
        {
            print("YOU DIED");
        }

        updateUI();
    }

    private void updateUI()
    {
        Health = Mathf.Clamp(Health, 0, 100f);
        Stamina = Mathf.Clamp(Stamina, 0, 100f);
        Hunger = Mathf.Clamp(Hunger, 0, 100f);
        Thirst = Mathf.Clamp(Thirst, 0, 100f);


        HealthSlider.value = Health;
        StaminaSlider.value = Stamina;
        HungerSlider.value = Hunger;
        ThirstSlider.value = Thirst;
    }

    public void TakeDamage(float amnt)
    {
        Health -= amnt;
        updateUI();
    }
}


What I have tried:

i've tried reading over it a few times but i cant see the issue.
Posted
Updated 3-Mar-21 22:20pm
v2

1 solution

i've tried reading over it a few times but i cant see the issue.

I don't think you have read over it a few times as the error is quite clear. Your code is trying to find a field called ThirstSlider but the field is clearly named incorrectly:
C#
public Slider ThirstSilder;

Fix the name of the field and the error should stop.
   
v2
Comments
BillWoodruff 4-Mar-21 4:35am
   
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900