- Home /
UI Slider, Save Values and Change Them
I have a UI slider which opens up when a user taps on a flag of Asia on a map. The slider has a min value of 0 and max of 500, value set at 500 and the direction is Left to Right. Beside the slider I have a UI Text which is assigned to the slider and shows 500 and will decrease if the slider is moved left and increase to 500 if moved right again. The problem I am having is I also have another script that has resources set up to show the player the value that they need. They will then go and move the slider to that value and Hit ok . Youll see clearer in the code below.
UI Slider Class
public class SliderUIMoneyTextUpdate : MonoBehaviour
{
//string sliderTextString = "500";
string sliderTextString;
public Text sliderMoneyText;
public static int sliderMoneyValue;
public int totalMoney;
void Start()
{
totalMoney = PlayerPrefs.GetInt ("money", totalMoney);
}
public void textUpdate(float textUpdateNumber)
{
sliderTextString = textUpdateNumber.ToString ();
sliderMoneyText.text = sliderTextString;
int.TryParse (sliderTextString, out sliderMoneyValue);
SaveMoney ();
}
public void SaveMoney()
{
totalMoney += sliderMoneyValue;
PlayerPrefs.SetInt ("money", totalMoney);
}
}
Resource Class:
public class MoreResourceUI : MonoBehaviour
{
public static int resourceMoney;
public int AsiaMoney;
//Assign the Needed Money to the Countries
public void asiamoney (Text asiaMoney)
{
resourceMoney = AsiaMoney;
AsiaMoney = 400;
asiaMoney.text = "Money: " + AsiaMoney; //Output required Money Amount
}
In my Asia_Resources script I have an if statement. I have tried the == and <=, < and > but all produce the same outcome as when you click on asia the slider value is set to 500 and therefore is always greater than money. I need it to be 0 even though it is displayed as 500 to the user and therefore being able to save the number they submit and tun it through the if statement again ! Any ideas how to do this ?
public class Asia_Resources : MonoBehaviour
{
public static int Money_Asia;
public static int Troops_Asia;
public static int Weapons_Asia;
public int money;
public int troops;
public int weapons;
public int totalMoney;
void Awake()
{
Money_Asia = 0;
Troops_Asia = 0;
Weapons_Asia = 0;
}
public void UpdateResources()
{
Money_Asia = PlayerPrefs.GetInt ("money");
MoreResourceUI UImoney = GetComponent<MoreResourceUI> ();
UImoney.AsiaMoney = money; //set at 400
if (Money_Asia >= money)
{
Debug.Log ("HAHAHAHA");
USA_Points.USAPoints += 1;
}
}
}
sorry.. i read your question several times and couldn't understand your issue. can you explain shortly or add an image or something? any way, if this related, slider value is a float. you should mark use whole number. or use float for you money.
Just so we can understand better. The purpose of these scripts is to store and display the values of the sliders for asia?
yea sorry guys bit confusing..yea i want to store and display values of asia which I am doing...the problem is as soon as the panel loads with the slider it carrys out that if statemtn ($$anonymous$$oney_Asia > money) due to the slider starting with the value 500 and the money having value of 400. I need that 500 value to basically not come into effect until i move the sliders and press the ok button, then do that if statement !
UpdateResources() is not invoked in your scripts.
It is very hard to help fix the logic in the script when the logic is so hard to follow.
The if statement is asking "is the Asia money value more than or equal to money" if it is add 1 to USA's points.
I find it very hard to understand what it is trying to do. Do you have any screenshots with some explaination on the purpose of the game/app?
Your answer
Follow this Question
Related Questions
Slider won't slide, issue assigning PlayerPrefs and then changing the PlayerPrefs' value 1 Answer
Updating UISlider with value from variable. 1 Answer
Wierd behaviour of slider when value is changed too fast 1 Answer
How to make if limit is reached then make sliders only slide back in ui (not forward) 2 Answers
Trying to move slider based off percentage of screen 1 Answer