- Home /
UI Slider looses control in Update function
Hello!
i'm having some issues with my UI slider that displays the energy of a character.
when an item is bought, I want the energy to move up only the value of the bought item. but it just fills it up to the end. I believe this is because of how Update is called once every frame and the button press might take longer than that to receive information and add the amount indicated. (i just don't know how to fix this)
essentially is there a better way to not have my code optimized so it doesn't do this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CharacterStats : MonoBehaviour
{
public static CharacterStats instace;
static protected int happinessValue = 100;
static protected float energyValue = 100;
static protected int smartValue = 100;
static protected int looksValue = 100;
static protected int money = 500;
[Header("Character UI")]
[SerializeField] private Slider happiness;
[SerializeField] private Slider energy;
[SerializeField] private Slider smart;
[SerializeField] private Slider looks;
[SerializeField] private GameObject moneyUI;
public int count = 0;
[HideInInspector]
private Vector3 last_pos;
private float damage;
void Start()
{
instance = this;
//----------------LOAD DATA-----------------
happiness.value = 45;
energy.value = energyValue;
smart.value = smartValue;
looks.value = looksValue;
last_pos = transform.position;
damage = 5;
//---------------------------------------------
}
void Update()
{
moneyUI.GetComponent<Text>().text = "" + money;
isWalking();
//this is where the purchase function is called
purchase();
}
void isWalking()
{
if (WorldInteraction.instance.velocity1[0] != 0 || WorldInteraction.instance.velocity1[1] != 0)
{
energy.value -= damage * (Vector3.Distance(transform.position , last_pos)) * Time.fixedDeltaTime;
happiness.value += (damage - 1) * (Vector3.Distance(transform.position, last_pos)) * Time.fixedDeltaTime;
last_pos = transform.position;
}
else
{
Debug.Log("not moving");
}
}
//here is the function that collects the amount of energy to add based on what is bought
public void purchase()
{
energy.value += ShopItemSearch.GetEnergy();
happiness.value += ShopItemSearch.GetEnergy() * -1;
Debug.Log("purchase" + ShopItemSearch.GetEnergy());
}
/*------------------------------------------------------------------------------------
//---------------------------------SAVING SCENE DATA-----------------------------------
/------------------------------------------------------------------------------------*/
void OnDestroy()
{
}
/*---------------------------STATS GETTERS AND SETTERS---------------------------*/
public static int GetMoney()
{
return money;
}
public static void SetMoney(int price)
{
money = price;
}
public static int GetHappiness()
{
return happinessValue;
}
public static void SetHappiness(int value)
{
happinessValue += value;
}
public static float GetEnergy()
{
return energyValue;
}
public static void SetEnergy(float value)
{
energyValue += value;
}
}
Answer by diwang · May 15, 2020 at 05:12 PM
If i wasn't wrong , this is your purchase()
energy.value += ShopItemSearch.GetEnergy();
happiness.value += ShopItemSearch.GetEnergy() * -1;
And this is your GetEnergy()
public static float GetEnergy()
{
return energyValue;
}
So it equals to this
energy.value += energyValue;
happiness.value += energyValue * -1;
And i see nowhere in your code that energyValue is reduced in any mean , so therefore the energy.value will keep rising because energyValue is never reduced .
Solution : set energyValue to zero after using it
energy.value += ShopItemSearch.GetEnergy();
happiness.value += ShopItemSearch.GetEnergy() * -1;
energyValue = 0;
Your answer
Follow this Question
Related Questions
Dynamic slider size with the new UI 2 Answers
left end of slider fill flattening the more i move the value down 0 Answers
Why hip will move upward and downward automatically when start to drag slider at x,y and z axis? 0 Answers
Rotatin the Camera using the UI slider value 1 Answer
How to put a image under a powerbar's handle thats moving? 0 Answers